forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0838-push-dominoes.kt
30 lines (27 loc) · 908 Bytes
/
0838-push-dominoes.kt
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
class Solution {
fun pushDominoes(d: String): String {
val domArr = d.toCharArray()
val q = LinkedList<Pair<Int, Char>>()
for ((i, v) in domArr.withIndex()) {
if (v != '.')
q.addLast(i to v)
}
while (q.isNotEmpty()) {
val (i, v) = q.removeFirst()
if (v == 'L' && i > 0 && domArr[i - 1] == '.') {
q.addLast(i - 1 to 'L')
domArr[i - 1] = 'L'
} else if (v == 'R') {
if (i + 1 < domArr.size && domArr[i + 1] == '.') {
if (i + 2 < domArr.size && domArr[i + 2] == 'L') {
q.removeFirst()
} else {
q.addLast(i + 1 to 'R')
domArr[i + 1] = 'R'
}
}
}
}
return String(domArr)
}
}