- Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_1625.java
43 lines (41 loc) · 1.43 KB
/
_1625.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
packagecom.fishercoder.solutions.secondthousand;
importjava.util.HashSet;
importjava.util.LinkedList;
importjava.util.Queue;
importjava.util.Set;
publicclass_1625 {
publicstaticclassSolution1 {
publicStringfindLexSmallestString(Strings, inta, intb) {
Queue<String> queue = newLinkedList<>();
Set<String> seen = newHashSet<>();
queue.offer(s);
Stringsmallest = s;
while (!queue.isEmpty()) {
Stringcurrent = queue.poll();
// add
char[] c = current.toCharArray();
for (inti = 1; i < c.length; i++) {
if (i % 2 == 1) {
c[i] = (char) (((Integer.parseInt(String.valueOf(c[i])) + a) % 10) + '0');
}
}
Stringnext = newString(c);
if (smallest.compareTo(next) > 0) {
smallest = next;
}
if (seen.add(next)) {
queue.add(next);
}
// rotate
next = next.substring(next.length() - b) + next.substring(0, next.length() - b);
if (seen.add(next)) {
queue.add(next);
}
if (smallest.compareTo(next) > 0) {
smallest = next;
}
}
returnsmallest;
}
}
}