-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackspace_string_compare.go
More file actions
68 lines (61 loc) · 1.1 KB
/
Copy pathbackspace_string_compare.go
File metadata and controls
68 lines (61 loc) · 1.1 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package leetcode
// Time complexity: O(n + m) where n and m are the lengths of s and t
// Space complexity: O(1)
func backspaceCompare(s string, t string) bool {
i, j := len(s)-1, len(t)-1
for i >= 0 || j >= 0 {
c := 0
for i >= 0 && (s[i] == '#' || c > 0) {
if s[i] == '#' {
c++
} else {
c--
}
i--
}
c = 0
for j >= 0 && (t[j] == '#' || c > 0) {
if t[j] == '#' {
c++
} else {
c--
}
j--
}
if i >= 0 && j >= 0 {
if s[i] != t[j] {
return false
}
} else {
break
}
i--
j--
}
return i < 0 && j < 0
}
// Time complexity: O(n + m) where n and m are the length of s and t
// Space complexity: O(n + m)
func backspaceCompare2(s string, t string) bool {
build := func(s string) []byte {
ss := make([]byte, 0, len(s))
for i := 0; i < len(s); i++ {
if s[i] == '#' && len(ss) > 0 {
ss = ss[:len(ss)-1]
} else if s[i] != '#' {
ss = append(ss, s[i])
}
}
return ss
}
ss, tt := build(s), build(t)
if len(ss) != len(tt) {
return false
}
for i := 0; i < len(ss); i++ {
if ss[i] != tt[i] {
return false
}
}
return true
}