-
Notifications
You must be signed in to change notification settings - Fork 0
/
20ValidParentheses.swift
81 lines (67 loc) · 2.02 KB
/
20ValidParentheses.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
/**
Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
An input string is valid if:
Open brackets must be closed by the same type of brackets.
Open brackets must be closed in the correct order.
Every close bracket has a corresponding open bracket of the same type.
Example 1:
Input: s = "()"
Output: true
Example 2:
Input: s = "()[]{}"
Output: true
Example 3:
Input: s = "(]"
Output: false
*/
class Solution {
func isValid(_ s: String) -> Bool {
var stack = [Character]()
for char in s {
if char == "(" || char == "{" || char == "[" {
stack.append(char)
} else if !stack.isEmpty {
let last = stack.removeLast()
if last == "(" && char == ")" {
continue
} else if last == "{" && char == "}" {
continue
} else if last == "[" && char == "]" {
continue
} else {
return false
}
} else {
return false
}
}
return stack.isEmpty
}
}
// Runtime 3 ms Beats 69.2%
// Memory 14.3 MB Beats 47.81%
// 0ms genius solution
class Solution {
func isValid(_ s: String) -> Bool {
var openSet : Set<Character> = ["(", "{", "["]
var closeSet : Set<Character> = [")", "}", "]"]
var map : [Character : Character] = ["(" : ")", "{" : "}", "[" : "]"]
var stack = [Character]()
let sArray = Array(s)
for c in sArray {
if openSet.contains(c) {
stack.append(c)
} else if closeSet.contains(c) {
if !stack.isEmpty {
let poppedChar = stack.removeLast()
if map[poppedChar] != c {
return false
}
}else {
return false
}
}
}
return stack.isEmpty
}
}