Skip to content

Commit 3101b94

Browse files
add 777
1 parent 6b26eba commit 3101b94

File tree

3 files changed

+139
-0
lines changed
  • paginated_contents/algorithms/1st_thousand
  • src
    • main/java/com/fishercoder/solutions/firstthousand
    • test/java/com/fishercoder/firstthousand

3 files changed

+139
-0
lines changed

Diff for: paginated_contents/algorithms/1st_thousand/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@
115115
| 783 | [Minimum Distance Between BST Nodes](https://leetcode.com/problems/minimum-distance-between-bst-nodes/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_783.java) | | Easy |
116116
| 781 | [Rabbits in Forest](https://leetcode.com/problems/rabbits-in-forest/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_781.java) | [:tv:](https://youtu.be/leiSa1i-QrI) | Medium | HashTable, Math
117117
| 779 | [K-th Symbol in Grammar](https://leetcode.com/problems/k-th-symbol-in-grammar/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_779.java) | | Medium |
118+
| 777 | [Swap Adjacent in LR String](https://leetcode.com/problems/swap-adjacent-in-lr-string/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_777.java) | | Medium | Two Pointers, String
118119
| 776 | [Split BST](https://leetcode.com/problems/split-bst/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_776.java) | | Medium | Recursion
119120
| 775 | [Global and Local Inversions](https://leetcode.com/problems/global-and-local-inversions/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_775.java) | | Medium | Array, Math
120121
| 773 | [Sliding Puzzle](https://leetcode.com/problems/sliding-puzzle/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_773.java) | | Hard | BFS
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.fishercoder.solutions.firstthousand;
2+
3+
public class _777 {
4+
public static class Solution1 {
5+
public boolean canTransform(String start, String end) {
6+
StringBuilder sb = new StringBuilder();
7+
for (char c : start.toCharArray()) {
8+
if (c != 'X') {
9+
sb.append(c);
10+
}
11+
}
12+
String cleanedStart = sb.toString();
13+
sb.setLength(0);
14+
for (char c : end.toCharArray()) {
15+
if (c != 'X') {
16+
sb.append(c);
17+
}
18+
}
19+
String cleandEnd = sb.toString();
20+
if (!cleanedStart.equals(cleandEnd)) {
21+
return false;
22+
}
23+
24+
//check R from left, check on the start string for R first
25+
//whenever count becomes negative, this means we encounter an R in a more left position in end string than start string
26+
//since R could only be moved to the right in the start string, there's no way that start string could be shifted to match end string
27+
//test11 illustrates this well
28+
int count = 0;
29+
for (int i = 0; i < start.length(); i++) {
30+
if (start.charAt(i) == 'R') {
31+
count++;
32+
}
33+
if (end.charAt(i) == 'R') {
34+
count--;
35+
}
36+
if (count < 0) {
37+
return false;
38+
}
39+
}
40+
//check L from left, but check on the end string first,
41+
//this means if L is in a more left index in start string than end string, it's impossible for start to match end
42+
//test12 illustrates this case well
43+
count = 0;
44+
for (int i = 0; i < end.length(); i++) {
45+
if (end.charAt(i) == 'L') {
46+
count++;
47+
}
48+
if (start.charAt(i) == 'L') {
49+
count--;
50+
}
51+
if (count < 0) {
52+
return false;
53+
}
54+
}
55+
return true;
56+
}
57+
58+
}
59+
}
+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.fishercoder.firstthousand;
2+
3+
import com.fishercoder.solutions.firstthousand._777;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.assertFalse;
8+
import static org.junit.jupiter.api.Assertions.assertTrue;
9+
10+
public class _777Test {
11+
12+
private static _777.Solution1 solution1;
13+
14+
@BeforeEach
15+
public void setup() {
16+
solution1 = new _777.Solution1();
17+
}
18+
19+
@Test
20+
public void test1() {
21+
assertTrue(solution1.canTransform("RXXLRXRXL", "XRLXXRRLX"));
22+
}
23+
24+
@Test
25+
public void test2() {
26+
assertFalse(solution1.canTransform("X", "L"));
27+
}
28+
29+
@Test
30+
public void test3() {
31+
assertTrue(solution1.canTransform("XXXXXLXXXX", "LXXXXXXXXX"));
32+
}
33+
34+
@Test
35+
public void test4() {
36+
assertFalse(solution1.canTransform("LLR", "RRL"));
37+
}
38+
39+
@Test
40+
public void test5() {
41+
assertFalse(solution1.canTransform("XXXL", "LXXR"));
42+
}
43+
44+
@Test
45+
public void test6() {
46+
assertTrue(solution1.canTransform("RXXX", "XXXR"));
47+
}
48+
49+
@Test
50+
public void test7() {
51+
assertTrue(solution1.canTransform("XXRXLXRXXX", "XXRLXXXXXR"));
52+
}
53+
54+
@Test
55+
public void test8() {
56+
assertFalse(solution1.canTransform("XLLR", "LXLX"));
57+
}
58+
59+
@Test
60+
public void test9() {
61+
assertFalse(solution1.canTransform("RXR", "XXR"));
62+
}
63+
64+
@Test
65+
public void test10() {
66+
assertTrue(solution1.canTransform("XLXRRXXRXX", "LXXXXXXRRR"));
67+
}
68+
69+
@Test
70+
public void test11() {
71+
assertFalse(solution1.canTransform("LXXLXRLXXL", "XLLXRXLXLX"));
72+
}
73+
74+
@Test
75+
public void test12() {
76+
assertFalse(solution1.canTransform("XXXXXLXXXLXXXX", "XXLXXXXXXXXLXX"));
77+
}
78+
79+
}

0 commit comments

Comments
 (0)