Backspace String Compare
看两个string是不是相同, string里的#号是删除下一个字符.
class Solution {
public boolean backspaceCompare(String S, String T) {
StringBuffer sb1 = new StringBuffer(S);
StringBuffer sb2 = new StringBuffer(T);
sb1.reverse();
sb2.reverse();
return modify(sb1).equals(modify(sb2));
}
private String modify(StringBuffer sb) {
int skip = 0;
StringBuffer tmp = new StringBuffer();
for(int i = 0 ; i < sb.length(); i++) {
if(sb.charAt(i) == '#') {
skip++;
continue;
}
else if(skip > 0){
skip--;
continue;
}
else{
tmp.append(sb.charAt(i));
}
}
System.out.println(tmp.toString());
return tmp.toString();
}
}