-
Notifications
You must be signed in to change notification settings - Fork 0
/
ValidParentheses.kt
39 lines (32 loc) · 1.12 KB
/
ValidParentheses.kt
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
package problems
import java.util.*
import kotlin.collections.HashMap
object ValidParentheses {
fun isValid(s: String): Boolean {
val inputSize = s.length
if ((inputSize % 2) == 1) return false //Has to be even to be true
val parenthesesKey = HashMap<Char, Char>()
parenthesesKey.put(')', '(')
parenthesesKey.put('}', '{')
parenthesesKey.put(']', '[')
val stack = Stack<Char>()
for (i in 0 until inputSize) {
val char = s[i]
if (parenthesesKey.containsKey(char)) {
//is a closing
val opening = parenthesesKey[char] //retrieve opening
if (stack.isNotEmpty() && stack.peek() == opening) {
stack.pop()
} else {
//means nothing is infront of the closing bracket
//Or what is infront of the closing bracket does not match itself
return false
}
} else {
//is a opening
stack.push(char)
}
}
return stack.size == 0
}
}