forked from LeetCode-in-Scala/LeetCode-in-Scala
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.scala
35 lines (30 loc) · 1.11 KB
/
Solution.scala
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
package g0001_0100.s0020_valid_parentheses
// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #String #Stack
// #Data_Structure_I_Day_9_Stack_Queue #Udemy_Strings #Big_O_Time_O(n)_Space_O(n)
// #2024_06_01_Time_587_ms_(69.29%)_Space_58.3_MB_(60.63%)
import scala.collection.mutable.Stack
import scala.util.control.Breaks._
object Solution {
def isValid(s: String): Boolean = {
val stack = Stack[Char]()
var result = true
breakable {
for (i <- 0 until s.length) {
val c = s.charAt(i)
if (c == '(' || c == '[' || c == '{') {
stack.push(c)
} else if (c == ')' && stack.nonEmpty && stack.top == '(') {
stack.pop()
} else if (c == '}' && stack.nonEmpty && stack.top == '{') {
stack.pop()
} else if (c == ']' && stack.nonEmpty && stack.top == '[') {
stack.pop()
} else {
result = false
break()
}
}
}
result && stack.isEmpty
}
}