Skip to content

Commit bb05275

Browse files
authored
Create LC_lexsmalleststrafterop.java
1 parent e00894c commit bb05275

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class LC_lexsmalleststrafterop {
2+
3+
public String findLexSmallestString(String s, int a, int b) {
4+
int n = s.length();
5+
boolean[] vis = new boolean[n];
6+
String res = s;
7+
// double the length of s for convenience in extracting the rotated string t
8+
s = s + s;
9+
for (int i = 0; !vis[i]; i = (i + b) % n) {
10+
vis[i] = true;
11+
for (int j = 0; j < 10; j++) {
12+
int kLimit = b % 2 == 0 ? 0 : 9;
13+
for (int k = 0; k <= kLimit; k++) {
14+
// before each accumulation, re-truncate t
15+
char[] t = s.substring(i, i + n).toCharArray();
16+
for (int p = 1; p < n; p += 2) {
17+
t[p] = (char) ('0' + ((t[p] - '0' + j * a) % 10));
18+
}
19+
for (int p = 0; p < n; p += 2) {
20+
t[p] = (char) ('0' + ((t[p] - '0' + k * a) % 10));
21+
}
22+
String tStr = new String(t);
23+
if (tStr.compareTo(res) < 0) {
24+
res = tStr;
25+
}
26+
}
27+
}
28+
}
29+
return res;
30+
}
31+
}

0 commit comments

Comments
 (0)