-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcode_844.java
38 lines (36 loc) · 1.14 KB
/
leetcode_844.java
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
package leetcode;
public class leetcode_844 {
public static void main(String[] args){
String s = "a##c", t = "#a#c";
Comp c =new Comp();
System.out.println(c.backspaceCompare(s,t));
}
}
/*
思路1:针对两字符串做处理,得到最后结果,再比较用到string类的一些方法
*/
class Comp{
public boolean backspaceCompare(String s, String t) {
int length_s= s.length(), length_t = t.length();
String s_r="", t_r="";
for(int i =0;i<length_s;i++){
if(s.charAt(i)== '#'&& s_r.length()>0){
s_r=s_r.substring(0,s_r.length()-1);
}
else if(s.charAt(i)== '#'&& s_r.length()==0)
s_r=s_r;
else
s_r+=s.charAt(i);
}
for(int i =0;i<length_t;i++){
if(t.charAt(i)== '#'&& t_r.length()>0){
t_r=t_r.substring(0,t_r.length()-1);
}
else if(t.charAt(i)== '#'&& t_r.length()==0)
t_r=t_r;
else
t_r+=t.charAt(i);
}
return s_r.equals(t_r);
}
}