844. Backspace String Compare
class Solution {
public boolean backspaceCompare(String S, String T) {
LinkedList s = new LinkedList();
LinkedList t = new LinkedList();
for (int i = 0; i < S.length(); i++) {
if (S.charAt(i) == '#') {
if (!s.isEmpty())
s.pop();
} else
s.push(S.charAt(i));
}
for (int i = 0; i < T.length(); i++) {
if (T.charAt(i) == '#') {
if (!t.isEmpty())
t.pop();
} else
t.push(T.charAt(i));
}
if (s.size() != t.size())
return false;
while (!s.isEmpty()) {
if (s.pop() != t.pop())
return false;
}
return true;
}
}
Like this:
Like Loading...
Related