-
Notifications
You must be signed in to change notification settings - Fork 5
/
leetcode-844-backspace-string-compare.swift
168 lines (122 loc) · 3.64 KB
/
leetcode-844-backspace-string-compare.swift
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/**
Copyright (c) 2020 David Seek, LLC
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
OR OTHER DEALINGS IN THE SOFTWARE.
https://leetcode.com/problems/backspace-string-compare/
Given two strings S and T, return if they are equal when both are typed into empty text editors. # means a backspace character.
Note that after backspacing an empty text, the text will continue empty.
Example 1:
Input: S = "ab#c", T = "ad#c"
Output: true
Explanation: Both S and T become "ac".
Example 2:
Input: S = "ab##", T = "c#d#"
Output: true
Explanation: Both S and T become "".
Example 3:
Input: S = "a##c", T = "#a#c"
Output: true
Explanation: Both S and T become "c".
Example 4:
Input: S = "a#c", T = "b"
Output: false
Explanation: S becomes "c" while T becomes "b".
Note:
1 <= S.length <= 200
1 <= T.length <= 200
S and T only contain lowercase letters and '#' characters.
Follow up:
Can you solve it in O(N) time and O(1) space?
*/
/**
Big O Annotation
Time complexity O(n) where n is the combined character length of S and T.
Space complexity O(n) where n is the combined character length of S and T.
*/
func backspaceCompare(_ S: String, _ T: String) -> Bool {
/**
Stacks to hold the characters of each S and T
*/
var sStack = Stack<Character>()
var tStack = Stack<Character>()
// Iterate through S
for character in S {
// And for every # pop the last..
if character == "#" {
// ... character in the stack
sStack.pop()
} else {
// Otherwise push the characters into the stack
sStack.push(character)
}
}
// Same as for S
for character in T {
if character == "#" {
tStack.pop()
} else {
tStack.push(character)
}
}
/**
Check if the amount of elements in the stack is equal
if not then there is no chance for us to create the same strings.
*/
guard sStack.count == tStack.count else {
return false
}
/**
We know the length of the stacks is equal
so we can iterate through either of them...
*/
for _ in 0..<sStack.count {
// To get the last elements of both stacks
let s = sStack.pop()
let t = tStack.pop()
// If they differ return false
guard s == t else {
return false
}
}
/**
If we fell through the iteration we
can return true as we've checked for proper equality.
*/
return true
}
struct Stack<Element: Equatable> {
var array: [Element]
init() {
array = []
}
public var isEmpty: Bool {
return array.isEmpty
}
public var peek: Element? {
return array.last
}
public var count: Int {
return array.count
}
mutating public func push(_ element: Element) {
array.append(element)
}
@discardableResult
mutating public func pop() -> Element? {
return array.popLast()
}
}
print(backspaceCompare("ab#c", "ad#c"))