-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_838.java
45 lines (43 loc) · 1.86 KB
/
_838.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
44
45
package com.fishercoder.solutions.firstthousand;
import java.util.HashSet;
import java.util.Set;
public class _838 {
public static class Solution1 {
public String pushDominoes(String dominoes) {
StringBuilder currentSb = new StringBuilder(dominoes);
Set<String> visited = new HashSet<>();
visited.add(dominoes);
do {
StringBuilder newSb = new StringBuilder();
for (int i = 0; i < currentSb.length(); i++) {
if (currentSb.charAt(i) == 'L') {
newSb.append('L');
if (i == 1 && currentSb.charAt(i - 1) == '.') {
newSb.replace(i - 1, i, "L");
} else if (i > 1
&& currentSb.charAt(i - 1) == '.'
&& currentSb.charAt(i - 2) != 'R') {
newSb.replace(i - 1, i, "L");
}
} else if (currentSb.charAt(i) == 'R') {
newSb.append('R');
if (i == currentSb.length() - 2 && currentSb.charAt(i + 1) == '.') {
newSb.replace(i + 1, i + 2, "R");
i++;
} else if (i < currentSb.length() - 2
&& currentSb.charAt(i + 1) == '.'
&& currentSb.charAt(i + 2) != 'L') {
newSb.replace(i + 1, i + 2, "R");
i++;
}
} else {
newSb.append('.');
}
}
currentSb.setLength(0);
currentSb = newSb;
} while (visited.add(currentSb.toString()));
return currentSb.toString();
}
}
}