-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPushDominoes_838.java
121 lines (107 loc) · 3.62 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import java.util.Scanner;
public class PushDominoes_838 {
public static char[] arrDominoes;
public static String pushDominoes(String dominoes) {
arrDominoes = dominoes.toCharArray();
int index = 0;
while (index < arrDominoes.length) {
char domino = arrDominoes[index];
if (domino == '.')
index++;
else {
if (domino == 'L') {
smashleftdominos(index - 1);
index++;
} else { // when domino is right 'R'
int leftIndex = findFirstLeft(index + 1);
int rightIndex = findFirstRight(index + 1);
if (rightIndex < leftIndex && rightIndex!=-1) {
smashRightdominos(index + 1);
index = rightIndex;
}
else if(rightIndex==-1 && leftIndex==-1){
smashRightdominos(index+1);
index++;
}
else {
smashDominosLeftRight(index, leftIndex);
index = leftIndex + 1;
}
}
}
}
StringBuilder builder = new StringBuilder();
for (char c : arrDominoes) {
builder.append(c);
}
return builder.toString();
}
public static void smashDominosLeftRight(int rightIndex, int leftIndex) {
int i = rightIndex;
int j = leftIndex;
boolean found = false;
while ((j - i) > 1 && !found && leftIndex < arrDominoes.length) { // check if RL
if (checkMoves(i, j)) { // check moves ar valid for both left and right by comparing the indexes
arrDominoes[i + 1] = 'R';
arrDominoes[j - 1] = 'L';
}
i++;
j--;
}
}
public static boolean checkMoves(int i, int j) {
return (j - 1) != (i + 1);
}
// Find the first right domino starting from begin index
public static int findFirstRight(int beginIndex) {
boolean found = false;
int index = beginIndex;
while (index < arrDominoes.length && !found) {
if (arrDominoes[index] == 'R')
found = true;
else
index++;
}
if(!found) index=-1;
return index;
}
public static int findFirstLeft(int beginIndex) {
boolean found = false;
int index = beginIndex;
while (index < arrDominoes.length && !found) {
if (arrDominoes[index] == 'L')
found = true;
else
index++;
}
if(!found) index=-1;
return index;
}
// Throw left dominos when we enconter our first left domino
public static void smashleftdominos(int index) {
if (index < 0 || arrDominoes[index] == 'L')
return;
else {
char domino = arrDominoes[index];
if (domino == '.')
;
arrDominoes[index] = 'L';
smashleftdominos(index - 1);
}
}
public static void smashRightdominos(int index) {
if (index > arrDominoes.length || arrDominoes[index-1] == 'R')
return;
else {
char domino = arrDominoes[index];
if (domino == '.')
;
arrDominoes[index] = 'R';
smashRightdominos(index + 1);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println(pushDominoes(sc.nextLine()));
}
}