Skip to content

Commit

Permalink
Create 0838-push-dominoes.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
a93a authored May 22, 2023
1 parent 86eb47b commit 9171260
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions kotlin/0838-push-dominoes.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,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)
}
}

0 comments on commit 9171260

Please sign in to comment.