-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackSpaceStringCompare.java
More file actions
53 lines (47 loc) · 1.14 KB
/
backSpaceStringCompare.java
File metadata and controls
53 lines (47 loc) · 1.14 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
42
43
44
45
46
47
48
49
50
51
52
53
class Solution
{
public boolean backspaceCompare(String S, String T)
{
char Sarr[]=S.toCharArray();
char Tarr[]=T.toCharArray();
Stack<Character> stack=new Stack<>();
for(int i=0;i<Sarr.length;i++)
{
if(Sarr[i]!='#')
{
stack.push(Sarr[i]);
}
else if(Sarr[i]=='#' && !stack.isEmpty()) //for cases such as a##a
{
stack.pop();
}
}
String newS="";
while(!stack.isEmpty())
{
newS=stack.pop()+newS;
}
for(int i=0;i<Tarr.length;i++)
{
if(Tarr[i]!='#')
{
stack.push(Tarr[i]);
}
else if(Tarr[i]=='#' && !stack.isEmpty())
{
stack.pop();
}
}
String newT="";
while(!stack.isEmpty())
{
newT=stack.pop()+newT;
}
if(newS.equals(newT))
{
return true;
}
return false;
//System.out.println(newS);
}
}