-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBackspaceStringCompare.java
71 lines (69 loc) · 1.97 KB
/
BackspaceStringCompare.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
Runtime: 0 ms, faster than 100.00% of Java online submissions for Backspace String Compare.
Memory Usage: 40.5 MB, less than 93.18% of Java online submissions for Backspace String Compare.
*/
// Optimized Solution TC: O(n) SC: O(1)
/*
TC: O(n) Explanation:
1. Because length of the String cannot be more than 200 as per the Question Constraints.
2. Hence, all the inner loops have a constant Time Complexity
*/
class Solution {
public boolean backspaceCompare(String s, String t) {
int i = s.length()-1;
int j = t.length()-1;
int hashCountS = 0;
int hashCountT = 0;
while(i>=0 || j >=0){
while(i >= 0){
if(s.charAt(i) == '#'){
hashCountS++;
i--;
}else if(hashCountS >0){
hashCountS--;
i--;
}else{
break;
}
}
while(j >= 0){
if(t.charAt(j) == '#'){
hashCountT++;
j--;
}else if(hashCountT >0){
hashCountT--;
j--;
}else{
break;
}
}
if(i >= 0 && j >= 0 && s.charAt(i) != t.charAt(j)){
return false;
}
if((i >= 0) != (j>=0)){
return false;
}
i--;
j--;
}
return true;
}
}
/*
//Brute Force TC: O(n) SC: O(n)
class Solution {
public boolean backspaceCompare(String s, String t) {
return buildString(s).equals(buildString(t));
}
public String buildString(String s){
LinkedList<Character> list = new LinkedList<>();
for(char c : s.toCharArray()){
if(c!='#')
list.add(c);
else if(!list.isEmpty())
list.removeLast();
}
return String.valueOf(list);
}
}
*/