Skip to content

Commit cf8de87

Browse files
add 838
1 parent 5773d5a commit cf8de87

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ _If you like this project, please leave me a star._ ★
350350
|844|[Backspace String Compare](https://leetcode.com/problems/backspace-string-compare/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_844.java) | |Easy|
351351
|841|[Keys and Rooms](https://leetcode.com/problems/keys-and-rooms/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_841.java) | |Easy|DFS, Graph
352352
|840|[Magic Squares In Grid](https://leetcode.com/problems/magic-squares-in-grid/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_840.java) | |Easy|
353+
|838|[Push Dominoes](https://leetcode.com/problems/push-dominoes/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_838.java) | |Medium| Two Pointers, DP
353354
|836|[Rectangle Overlap](https://leetcode.com/problems/rectangle-overlap/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_836.java) | [:tv:](https://youtu.be/o6hHUk4DOW0) |Easy|
354355
|832|[Flipping an Image](https://leetcode.com/problems/flipping-an-image/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_832.java) | |Easy|
355356
|830|[Positions of Large Groups](https://leetcode.com/problems/positions-of-large-groups/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_830.java) | |Easy|
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
public class _838 {
7+
public static class Solution1 {
8+
public String pushDominoes(String dominoes) {
9+
StringBuilder currentSb = new StringBuilder(dominoes);
10+
Set<String> visited = new HashSet<>();
11+
visited.add(dominoes);
12+
do {
13+
StringBuilder newSb = new StringBuilder();
14+
for (int i = 0; i < currentSb.length(); i++) {
15+
if (currentSb.charAt(i) == 'L') {
16+
newSb.append('L');
17+
if (i == 1 && currentSb.charAt(i - 1) == '.') {
18+
newSb.replace(i - 1, i, "L");
19+
} else if (i > 1 && currentSb.charAt(i - 1) == '.' && currentSb.charAt(i - 2) != 'R') {
20+
newSb.replace(i - 1, i, "L");
21+
}
22+
} else if (currentSb.charAt(i) == 'R') {
23+
newSb.append('R');
24+
if (i == currentSb.length() - 2 && currentSb.charAt(i + 1) == '.') {
25+
newSb.replace(i + 1, i + 2, "R");
26+
i++;
27+
} else if (i < currentSb.length() - 2 && currentSb.charAt(i + 1) == '.' && currentSb.charAt(i + 2) != 'L') {
28+
newSb.replace(i + 1, i + 2, "R");
29+
i++;
30+
}
31+
} else {
32+
newSb.append('.');
33+
}
34+
}
35+
currentSb.setLength(0);
36+
currentSb = newSb;
37+
} while (visited.add(currentSb.toString()));
38+
return currentSb.toString();
39+
}
40+
}
41+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._838;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _838Test {
10+
private static _838.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _838.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals("LL.RR.LLRRLL..", solution1.pushDominoes(".L.R...LR..L.."));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals("RR.L", solution1.pushDominoes("RR.L"));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(".", solution1.pushDominoes("."));
30+
}
31+
32+
@Test
33+
public void test4() {
34+
assertEquals("..RRR", solution1.pushDominoes("..R.."));
35+
}
36+
37+
@Test
38+
public void test5() {
39+
assertEquals("RRR.L", solution1.pushDominoes("R.R.L"));
40+
}
41+
42+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy