Skip to content

Commit f938d33

Browse files
authored
valid parentheses solution
1 parent f72c94c commit f938d33

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

valid-parentheses/yhkee0404.scala

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import scala.collection.mutable.ArrayBuffer
2+
3+
object Solution {
4+
var encoder = "(){}[]".zipWithIndex
5+
.toMap
6+
def isValid(s: String): Boolean = {
7+
val arr = ArrayBuffer[Int]()
8+
s.forall { c =>
9+
val x = encoder(c)
10+
if ((x & 1) == 0) {
11+
arr += x
12+
true
13+
} else {
14+
arr.lastOption match {
15+
case Some(y) => {
16+
if (y == x - 1) {
17+
arr.dropRightInPlace (1)
18+
true
19+
} else false
20+
}
21+
case None => false
22+
}
23+
}
24+
} && arr.isEmpty
25+
}
26+
}

0 commit comments

Comments
 (0)