Skip to content

Commit baf6fe2

Browse files
authored
Merge pull request #1438 from JustHm/main
[JustHm] Week 06 Solutions
2 parents c204b0b + 0e32552 commit baf6fe2

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

valid-parentheses/JustHm.swift

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// time: O(n) space: O(n)
2+
class Solution {
3+
func isValid(_ s: String) -> Bool {
4+
var answer = [Character]()
5+
for char in s {
6+
if let recent = answer.last {
7+
if char == ")" && recent == "(" {
8+
answer.removeLast()
9+
continue
10+
}
11+
else if char == "]" && recent == "[" {
12+
answer.removeLast()
13+
continue
14+
}
15+
else if char == "}" && recent == "{" {
16+
answer.removeLast()
17+
continue
18+
}
19+
}
20+
answer.append(char)
21+
}
22+
return answer.isEmpty
23+
}
24+
}

0 commit comments

Comments
 (0)