forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1625.java
43 lines (41 loc) · 1.41 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
package com.fishercoder.solutions;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Set;
public class _1625 {
public static class Solution1 {
public String findLexSmallestString(String s, int a, int b) {
Queue<String> queue = new LinkedList<>();
Set<String> seen = new HashSet<>();
queue.offer(s);
String smallest = s;
while (!queue.isEmpty()) {
String current = queue.poll();
//add
char[] c = current.toCharArray();
for (int i = 1; i < c.length; i++) {
if (i % 2 == 1) {
c[i] = (char) (((Integer.parseInt(String.valueOf(c[i])) + a) % 10) + '0');
}
}
String next = new String(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;
}
}
return smallest;
}
}
}