|
| 1 | +// Source : https://leetcode.com/problems/lexicographically-smallest-string-after-applying-operations/ |
| 2 | +// Author : Hao Chen |
| 3 | +// Date : 2021-03-24 |
| 4 | + |
| 5 | +/***************************************************************************************************** |
| 6 | + * |
| 7 | + * You are given a string s of even length consisting of digits from 0 to 9, and two integers a and b. |
| 8 | + * |
| 9 | + * You can apply either of the following two operations any number of times and in any order on s: |
| 10 | + * |
| 11 | + * Add a to all odd indices of s (0-indexed). Digits post 9 are cycled back to 0. For example, |
| 12 | + * if s = "3456" and a = 5, s becomes "3951". |
| 13 | + * Rotate s to the right by b positions. For example, if s = "3456" and b = 1, s becomes |
| 14 | + * "6345". |
| 15 | + * |
| 16 | + * Return the lexicographically smallest string you can obtain by applying the above operations any |
| 17 | + * number of times on s. |
| 18 | + * |
| 19 | + * A string a is lexicographically smaller than a string b (of the same length) if in the first |
| 20 | + * position where a and b differ, string a has a letter that appears earlier in the alphabet than the |
| 21 | + * corresponding letter in b. For example, "0158" is lexicographically smaller than "0190" because the |
| 22 | + * first position they differ is at the third letter, and '5' comes before '9'. |
| 23 | + * |
| 24 | + * Example 1: |
| 25 | + * |
| 26 | + * Input: s = "5525", a = 9, b = 2 |
| 27 | + * Output: "2050" |
| 28 | + * Explanation: We can apply the following operations: |
| 29 | + * Start: "5525" |
| 30 | + * Rotate: "2555" |
| 31 | + * Add: "2454" |
| 32 | + * Add: "2353" |
| 33 | + * Rotate: "5323" |
| 34 | + * Add: "5222" |
| 35 | + * Add: "5121" |
| 36 | + * Rotate: "2151" |
| 37 | + * Add: "2050" |
| 38 | + * There is no way to obtain a string that is lexicographically smaller then "2050". |
| 39 | + * |
| 40 | + * Example 2: |
| 41 | + * |
| 42 | + * Input: s = "74", a = 5, b = 1 |
| 43 | + * Output: "24" |
| 44 | + * Explanation: We can apply the following operations: |
| 45 | + * Start: "74" |
| 46 | + * Rotate: "47" |
| 47 | + * Add: "42" |
| 48 | + * Rotate: "24" |
| 49 | + * There is no way to obtain a string that is lexicographically smaller then "24". |
| 50 | + * |
| 51 | + * Example 3: |
| 52 | + * |
| 53 | + * Input: s = "0011", a = 4, b = 2 |
| 54 | + * Output: "0011" |
| 55 | + * Explanation: There are no sequence of operations that will give us a lexicographically smaller |
| 56 | + * string than "0011". |
| 57 | + * |
| 58 | + * Example 4: |
| 59 | + * |
| 60 | + * Input: s = "43987654", a = 7, b = 3 |
| 61 | + * Output: "00553311" |
| 62 | + * |
| 63 | + * Constraints: |
| 64 | + * |
| 65 | + * 2 <= s.length <= 100 |
| 66 | + * s.length is even. |
| 67 | + * s consists of digits from 0 to 9 only. |
| 68 | + * 1 <= a <= 9 |
| 69 | + * 1 <= b <= s.length - 1 |
| 70 | + ******************************************************************************************************/ |
| 71 | + |
| 72 | +class Solution { |
| 73 | +private: |
| 74 | + unordered_map<string, bool> processed; |
| 75 | + void rotate_str(string& s, int n) { |
| 76 | + std::rotate(s.begin(), s.begin()+n, s.end()); |
| 77 | + } |
| 78 | + void add_str(string& s, int n) { |
| 79 | + for(int i=1; i<s.size(); i+=2) { |
| 80 | + s[i] = (s[i] -'0' + n) % 10 +'0'; |
| 81 | + } |
| 82 | + } |
| 83 | +public: |
| 84 | + string findLexSmallestString(string s, int a, int b) { |
| 85 | + string result = s; |
| 86 | + dfs(s, a, b, result); |
| 87 | + return result; |
| 88 | + } |
| 89 | + |
| 90 | + void dfs(string& s, int a, int b, string& result) { |
| 91 | + |
| 92 | + if (processed.find(s) != processed.end()) return; |
| 93 | + processed[s] = true; |
| 94 | + |
| 95 | + if (s < result) result = s; |
| 96 | + |
| 97 | + string str = s; |
| 98 | + rotate_str(str, b); |
| 99 | + dfs(str, a, b, result); |
| 100 | + |
| 101 | + str = s; |
| 102 | + add_str(str, a); |
| 103 | + dfs(str, a, b, result); |
| 104 | + |
| 105 | + } |
| 106 | +}; |
0 commit comments