-
Notifications
You must be signed in to change notification settings - Fork 0
/
PushDominoes_838.java
59 lines (56 loc) · 2.06 KB
/
PushDominoes_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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package org.example;
import java.util.ArrayList;
public class PushDominoes_838 {
class Solution {
public String pushDominoes(String dominoes) {
StringBuilder sb = new StringBuilder();
ArrayList<Character> stack = new ArrayList<>();
for (int i=0;i<dominoes.length();i++){
char c = dominoes.charAt(i);
if (c == '.'){
stack.add(c);
}
else {
int count = 0;
while (stack.size()>0 && stack.get(stack.size()-1) == '.'){
stack.remove(stack.size()-1);
count++;
}
if (stack.size()==0){
if (c == 'L'){
sb.append("L".repeat(Math.max(0, count + 1)));
}
else {
sb.append(".".repeat(Math.max(0, count)));
stack.add('R');
}
}
else {
if (c == 'L'){
stack.remove(stack.size()-1);
int RNum = count/2;
int LNum = count/2;
sb.append("R".repeat(Math.max(0, RNum+1)));
if (count%2!=0){
sb.append(".");
}
sb.append("L".repeat(Math.max(0, LNum+1)));
}
else {
sb.append("R".repeat(Math.max(0, count+1)));
stack.add('R');
}
}
}
}
System.out.println(stack);
if (stack.size()>0 && stack.get(0)=='R'){
sb.append("R".repeat(stack.size()));
}
else {
sb.append(".".repeat(stack.size()));
}
return sb.toString();
}
}
}