Skip to content

Commit 1007253

Browse files
committed
🎉 feat: initial commit for leetcode 20 using Swift
1 parent 7d5da0a commit 1007253

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Swift/20. Valid Parentheses.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// 20. Valid Parentheses
2+
// 16 ms, 74.38%
3+
func isValid(_ s: String) -> Bool {
4+
var stack = [Character](), b = true
5+
6+
func compare(_ c: Character) -> Bool {
7+
guard let t = stack.popLast() else { return false }
8+
return (c == ")" && t == "(") || (c == "]" && t == "[" ) || (c == "}" && t == "{")
9+
}
10+
11+
loop: for c in s {
12+
switch c {
13+
case "(", "[", "{":
14+
stack.append(c)
15+
default:
16+
b = compare(c)
17+
if !b { break loop }
18+
}
19+
}
20+
21+
return b && stack.isEmpty
22+
}

0 commit comments

Comments
 (0)