-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBackspaceString.java
More file actions
41 lines (36 loc) · 1.04 KB
/
Copy pathBackspaceString.java
File metadata and controls
41 lines (36 loc) · 1.04 KB
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
32
33
34
35
36
37
38
39
40
41
import java.util.*;
public class BackspaceString {
public static boolean backspaceCompare(String s, String t) {
System.out.println(nettstring(s));
System.out.println(nettstring(t));
return true;
}
public static String nettstring(String s){
StringBuffer nett=new StringBuffer();
Stack<Character>stack=new Stack<>();
// for(int i=s.length()-1;i>=0;i--){
// stack.push(s.charAt((i)));
// }
for(int i=0;i<s.length();i++){
// stack.push(s.charAt((i)));
if(s.charAt(i)=='#'){
if(!stack.isEmpty()){
stack.pop();
// System.out.println(stack.pop());
continue;
}
}
stack.push(s.charAt(i));
}
System.out.println(stack);
String res1 = "";
// String res2 = "";
while(!stack.isEmpty())
res1 += stack.pop();
return res1;
// return null;
}
public static void main(String[] args) {
backspaceCompare("ab#c","ad#c");
}
}