-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathleetcode-844-backspaceStringCompare.js
63 lines (55 loc) · 1.09 KB
/
leetcode-844-backspaceStringCompare.js
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
/**
* @param {string} s
* @param {string} t
* @return {boolean}
*/
// p: str, str
// r: boolean
// i
// ab#c
// j
// ad#c
var backspaceCompare = function (s, t) {
let i = s.length - 1,
j = t.length - 1;
let skipS = 0,
skipT = 0;
while (i >= 0 || j >= 0) {
while (i >= 0) {
console.log(skipS, i, skipT, j);
if (s[i] === "#") {
skipS++;
i--;
} else if (skipS > 0) {
skipS--;
i--;
} else break;
}
while (j >= 0) {
if (t[j] === "#") {
skipT++;
j--;
} else if (skipT > 0) {
skipT--;
j--;
} else break;
}
if (i >= 0 && j >= 0 && s[i] !== t[j]) return false;
if (i >= 0 !== j >= 0) return false;
i--;
j--;
}
return true;
};
// O(n) O(n)
// var backspaceCompare = function(s, t) {
// return getStr(s) === getStr(t);
// };
// const getStr = (str) => {
// let stack = [];
// for (let c of str) {
// c === '#' ? stack.pop() : stack.push(c);
// }
// return stack.join('');
// }
console.log(backspaceCompare("aaa###", "a#a#a#"));