Skip to content

Commit 3028683

Browse files
solves backspace string compare
1 parent 1fc4ec0 commit 3028683

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@
227227
| 832 | [Flipping an Image](https://leetcode.com/problems/flipping-an-image) | [![Java](assets/java.png)](src/FlippingAnImage.java) |
228228
| 836 | [Rectangle Overlap](https://leetcode.com/problems/rectangle-overlap) | [![Java](assets/java.png)](src/RectangleOverlap.java) |
229229
| 840 | [Magic Squares in Grid](https://leetcode.com/problems/magic-squares-in-grid) | |
230-
| 844 | [Backspace String Compare](https://leetcode.com/problems/backspace-string-compare) | |
230+
| 844 | [Backspace String Compare](https://leetcode.com/problems/backspace-string-compare) | [![Java](assets/java.png)](src/BackspaceStringCompare.java) |
231231
| 849 | [Maximize Distance to Closest Person](https://leetcode.com/problems/maximize-distance-to-closest-person) | |
232232
| 852 | [Peak Index in Mountain Array](https://leetcode.com/problems/peak-index-in-a-mountain-array) | |
233233
| 859 | [Buddy Strings](https://leetcode.com/problems/buddy-strings) | |

Diff for: src/BackspaceStringCompare.java

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
public class BackspaceStringCompare {
2+
public boolean backspaceCompare(String s, String t) {
3+
int i = nextCharIndex(s, s.length()), j = nextCharIndex(t, t.length());
4+
while (i >= 0 && j >= 0) {
5+
if (s.charAt(i) != t.charAt(j)) return false;
6+
i = nextCharIndex(s, i);
7+
j = nextCharIndex(t, j);
8+
}
9+
return i == -1 && j == -1;
10+
}
11+
12+
private int nextCharIndex(String string, int currentIndex) {
13+
for (int buffer = 0 ; buffer >= 0 ; currentIndex--) {
14+
if (currentIndex == 0) {
15+
currentIndex = -1;
16+
break;
17+
}
18+
if (string.charAt(currentIndex - 1) == '#') buffer++;
19+
else buffer--;
20+
}
21+
return currentIndex;
22+
}
23+
}

0 commit comments

Comments
 (0)