Backspace String Compare LeetCode solution in Java

1
class Solution {
2
public boolean backspaceCompare(String s, String t) {
3
int i = s.length() - 1, j = t.length() - 1;
4
while (i >= 0 || j >= 0) {
5
i = getCurPos(i, s);
6
j = getCurPos(j, t);
7
if (i >= 0 && j >= 0 && s.charAt(i) != t.charAt(j)) return false;
8
if ((i >= 0) != (j >= 0)) return false;
9
i--;
10
j--;
11
}
12
return true;
13
}
14

15
private int getCurPos(int i, String s) {
16
int dels = 0;
17
while (i >= 0) {
18
if (s.charAt(i) == '#') {
19
dels++;
20
i--;
21
} else if (dels > 0) {
22
dels--;
23
i--;
24
} else break;
25
}
26
return i;
27
}
28
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0