-
Notifications
You must be signed in to change notification settings - Fork 5
/
leetcode-20-valid-parentheses.swift
144 lines (102 loc) · 3.09 KB
/
leetcode-20-valid-parentheses.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
/**
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/valid-parentheses/
Given a string 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.
Note that an empty string is also considered valid.
Example 1:
Input: "()"
Output: true
Example 2:
Input: "()[]{}"
Output: true
Example 3:
Input: "(]"
Output: false
Example 4:
Input: "([)]"
Output: false
Example 5:
Input: "{[]}"
Output: true
*/
/**
Big O Annotation
Time complexity O(n) where n is the amount of characters in s.
Space complexity O(n log n) as we're
always at most holding 1/2 of characters in n
*/
func isValid(_ s: String) -> Bool {
// Init a Character Stack
var stack = Stack<Character>()
// Iterate through given string
for character in s {
// Check if the character is (, { or [
if isOpen(character) {
// If so put into the stack
stack.push(character)
} else {
// Pop the last element of the stack
guard let element = stack.pop() else {
return false
}
// Check if it's the desired counter part
if !isOpposite(character, of: element) {
return false
}
}
}
// Check if the Stack is empty
// If not then we did not close all brackets
return stack.isEmpty
}
func isOpen(_ character: Character) -> Bool {
return character == "(" ||
character == "{" ||
character == "["
}
func isOpposite(_ character: Character, of stackElement: Character) -> Bool {
if character == ")" {
return stackElement == "("
}
if character == "}" {
return stackElement == "{"
}
// character must be ]
return stackElement == "["
}
struct Stack<Element: Equatable> {
private var array: [Element]
public var peek: Element? {
return array.last
}
public var isEmpty: Bool {
return array.isEmpty
}
init() {
array = []
}
mutating public func push(_ element: Element) {
array.append(element)
}
mutating public func pop() -> Element? {
return array.popLast()
}
}