-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackSpaceStringCompare.java
More file actions
31 lines (27 loc) · 944 Bytes
/
Copy pathBackSpaceStringCompare.java
File metadata and controls
31 lines (27 loc) · 944 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//https://leetcode.com/problems/backspace-string-compare/
public class BackSpaceStringCompare {
public boolean backspaceCompare(String s, String t) {
char[] schs=s.toCharArray();
char[] tchs=t.toCharArray();
StringBuilder sb= new StringBuilder();
StringBuilder tb=new StringBuilder();
return createString(s,sb,schs).equals(createString(t,tb,tchs));
// return false;
}
//from the back count number of backspaces done and if cnt of backspace is 0 append to the result string
public String createString(String s, StringBuilder sb,char[] schs) {
int bk=0;
for(int i=s.length()-1;i>=0;i--) {
if(schs[i]=='#') {
bk++;
} else {
if(bk!=0) {
bk--;
} else {
sb.append(schs[i]);
}
}
}
return sb.toString();
}
}