Skip to content

Commit

Permalink
Merge pull request neetcode-gh#2579 from leahjia/0838-push-dominoes-java
Browse files Browse the repository at this point in the history
Create 0838-push-dominoes.java
  • Loading branch information
MHamiid authored Jun 15, 2023
2 parents 9d668c6 + 0679f9f commit d6c844d
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions java/0838-push-dominoes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Solution {
public String pushDominoes(String dominoes) {
int len = dominoes.length();
Queue<Integer> q = new LinkedList<>();
char[] dom = dominoes.toCharArray();
for (int i = 0; i < len; i++)
if (dominoes.charAt(i) != '.') q.offer(i);

while (!q.isEmpty()) {
int i = q.poll();
char ch = dom[i];
if (dom[i] == 'R') {
if (i + 1 < len && dom[i + 1] == '.') {
if (i + 2 < len && dom[i + 2] == 'L') {
q.poll();
} else {
dom[i + 1] = 'R';
q.offer(i + 1);
}
}
} else if (i > 0 && dom[i - 1] == '.') {
dom[i - 1] = 'L';
q.offer(i - 1);
}
}
return String.valueOf(dom);
}
}

0 comments on commit d6c844d

Please sign in to comment.