{"id":7528,"date":"2020-10-18T11:00:58","date_gmt":"2020-10-18T18:00:58","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7528"},"modified":"2020-10-18T11:05:32","modified_gmt":"2020-10-18T18:05:32","slug":"leetcode-1625-lexicographically-smallest-string-after-applying-operations","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-1625-lexicographically-smallest-string-after-applying-operations\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1625. Lexicographically Smallest String After Applying Operations"},"content":{"rendered":"\n<p>You are given a string&nbsp;<code>s<\/code>&nbsp;of&nbsp;<strong>even length<\/strong>&nbsp;consisting of digits from&nbsp;<code>0<\/code>&nbsp;to&nbsp;<code>9<\/code>, and two integers&nbsp;<code>a<\/code>&nbsp;and&nbsp;<code>b<\/code>.<\/p>\n\n\n\n<p>You can apply either of the following two operations any number of times and in any order on&nbsp;<code>s<\/code>:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Add&nbsp;<code>a<\/code>&nbsp;to all odd indices of&nbsp;<code>s<\/code>&nbsp;<strong>(0-indexed)<\/strong>. Digits post&nbsp;<code>9<\/code>&nbsp;are cycled back to&nbsp;<code>0<\/code>. For example, if&nbsp;<code>s = \"3456\"<\/code>&nbsp;and&nbsp;<code>a = 5<\/code>,&nbsp;<code>s<\/code>&nbsp;becomes&nbsp;<code>\"3951\"<\/code>.<\/li><li>Rotate&nbsp;<code>s<\/code>&nbsp;to the right by&nbsp;<code>b<\/code>&nbsp;positions. For example, if&nbsp;<code>s = \"3456\"<\/code>&nbsp;and&nbsp;<code>b = 1<\/code>,&nbsp;<code>s<\/code>&nbsp;becomes&nbsp;<code>\"6345\"<\/code>.<\/li><\/ul>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>lexicographically smallest<\/strong>&nbsp;string you can obtain by applying the above operations any number of times on<\/em>&nbsp;<code>s<\/code>.<\/p>\n\n\n\n<p>A string&nbsp;<code>a<\/code>&nbsp;is lexicographically smaller than a string&nbsp;<code>b<\/code>&nbsp;(of the same length) if in the first position where&nbsp;<code>a<\/code>&nbsp;and&nbsp;<code>b<\/code>&nbsp;differ, string&nbsp;<code>a<\/code>&nbsp;has a letter that appears earlier in the alphabet than the corresponding letter in&nbsp;<code>b<\/code>. For example,&nbsp;<code>\"0158\"<\/code>&nbsp;is lexicographically smaller than&nbsp;<code>\"0190\"<\/code>&nbsp;because the first position they differ is at the third letter, and&nbsp;<code>'5'<\/code>&nbsp;comes before&nbsp;<code>'9'<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"5525\", a = 9, b = 2\n<strong>Output:<\/strong> \"2050\"\n<strong>Explanation: <\/strong>We can apply the following operations:\nStart:  \"5525\"\nRotate: \"2555\"\nAdd:    \"2454\"\nAdd:    \"2353\"\nRotate: \"5323\"\nAdd:    \"5222\"\n\u200b\u200b\u200b\u200b\u200b\u200b\u200bAdd:    \"5121\"\n\u200b\u200b\u200b\u200b\u200b\u200b\u200bRotate: \"2151\"\n\u200b\u200b\u200b\u200b\u200b\u200b\u200bAdd:    \"2050\"\u200b\u200b\u200b\u200b\u200b\u200b\u200b\u200b\u200b\u200b\u200b\u200b\nThere is no way to obtain a string that is lexicographically smaller then \"2050\".\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"74\", a = 5, b = 1\n<strong>Output:<\/strong> \"24\"\n<strong>Explanation: <\/strong>We can apply the following operations:\nStart:  \"74\"\nRotate: \"47\"\n\u200b\u200b\u200b\u200b\u200b\u200b\u200bAdd:    \"42\"\n\u200b\u200b\u200b\u200b\u200b\u200b\u200bRotate: \"24\"\u200b\u200b\u200b\u200b\u200b\u200b\u200b\u200b\u200b\u200b\u200b\u200b\nThere is no way to obtain a string that is lexicographically smaller then \"24\".\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"0011\", a = 4, b = 2\n<strong>Output:<\/strong> \"0011\"\n<strong>Explanation: <\/strong>There are no sequence of operations that will give us a lexicographically smaller string than \"0011\".\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"43987654\", a = 7, b = 3\n<strong>Output:<\/strong> \"00553311\"\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>2 &lt;= s.length &lt;= 100<\/code><\/li><li><code>s.length<\/code>&nbsp;is even.<\/li><li><code>s<\/code>&nbsp;consists of digits from&nbsp;<code>0<\/code>&nbsp;to&nbsp;<code>9<\/code>&nbsp;only.<\/li><li><code>1 &lt;= a &lt;= 9<\/code><\/li><li><code>1 &lt;= b &lt;= s.length - 1<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Search<\/strong><\/h2>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  string findLexSmallestString(string s, int a, int b) {\n    unordered_set<string> seen;    \n    string ans(s);\n    function<void(string)> dfs = [&](const string& s) {\n      if (!seen.insert(s).second) return;\n      ans = min(ans, s);\n      string t(s);\n      for (int i = 1; i < s.length(); i += 2)\n        t[i] = (t[i] - '0' + a) % 10 + '0';\n      dfs(t);\n      dfs(s.substr(b) + s.substr(0, b));\n    };    \n    dfs(s);\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a string&nbsp;s&nbsp;of&nbsp;even length&nbsp;consisting of digits from&nbsp;0&nbsp;to&nbsp;9, and two integers&nbsp;a&nbsp;and&nbsp;b. You can apply either of the following two operations any number of times&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44],"tags":[33,177,42,4],"class_list":["post-7528","post","type-post","status-publish","format-standard","hentry","category-searching","tag-dfs","tag-medium","tag-search","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7528","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=7528"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7528\/revisions"}],"predecessor-version":[{"id":7530,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7528\/revisions\/7530"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7528"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7528"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7528"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}