diff --git a/src/test/java/LeetCode/Lexicographically_Smallest_String_After_Applying_Operations/LC_lexsmalleststrafterop.java b/src/test/java/LeetCode/Lexicographically_Smallest_String_After_Applying_Operations/LC_lexsmalleststrafterop.java new file mode 100644 index 00000000..1d2565ad --- /dev/null +++ b/src/test/java/LeetCode/Lexicographically_Smallest_String_After_Applying_Operations/LC_lexsmalleststrafterop.java @@ -0,0 +1,31 @@ +class LC_lexsmalleststrafterop { + + public String findLexSmallestString(String s, int a, int b) { + int n = s.length(); + boolean[] vis = new boolean[n]; + String res = s; + // double the length of s for convenience in extracting the rotated string t + s = s + s; + for (int i = 0; !vis[i]; i = (i + b) % n) { + vis[i] = true; + for (int j = 0; j < 10; j++) { + int kLimit = b % 2 == 0 ? 0 : 9; + for (int k = 0; k <= kLimit; k++) { + // before each accumulation, re-truncate t + char[] t = s.substring(i, i + n).toCharArray(); + for (int p = 1; p < n; p += 2) { + t[p] = (char) ('0' + ((t[p] - '0' + j * a) % 10)); + } + for (int p = 0; p < n; p += 2) { + t[p] = (char) ('0' + ((t[p] - '0' + k * a) % 10)); + } + String tStr = new String(t); + if (tStr.compareTo(res) < 0) { + res = tStr; + } + } + } + } + return res; + } +}