Skip to content

Commit 4330135

Browse files
author
tumanob
committed
Add solution scala for "20 Valid Parentheses"
1 parent 7afa074 commit 4330135

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

ScalaSolutions/src/main/scala/TwoSum_p1/Solution.scala

+9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
package TwoSum_p1
22

3+
/**
4+
* https://leetcode.com/problems/two-sum/
5+
* Given an array of integers nums and an integer target,
6+
* return indices of the two numbers such that they add up to target.
7+
* You may assume that each input would have exactly one solution,
8+
* and you may not use the same element twice.
9+
* You can return the answer in any order.
10+
* @tags: easy
11+
*/
312
object Solution {
413

514
def main(args: Array[String]): Unit = {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package ValidParentheses_p20
2+
3+
import scala.collection.mutable.Stack
4+
5+
/**
6+
* https://leetcode.com/problems/valid-parentheses/
7+
* Given a string s containing just the characters '(', ')', '{', '}', '[' and ']',
8+
* determine if the input string is valid.
9+
* An input string is valid if:
10+
* Open brackets must be closed by the same type of brackets
11+
* Open brackets must be closed in the correct order.
12+
*
13+
* @tags: easy
14+
*/
15+
object Solution {
16+
17+
val OpenToClose: Map[Char, Char] = Map('{' -> '}', '[' -> ']', '(' -> ')')
18+
val CloseToOpen: Map[Char, Char] = OpenToClose.map(_.swap)
19+
20+
def main(args: Array[String]): Unit = {
21+
println(!isValid("["))
22+
println(isValid("()[]{}")) // all should be true
23+
println(isValid("()"))
24+
println(!isValid("(]"))
25+
println(isValid("[()]"))
26+
println(isValid("{[()]}"))
27+
println(isValid("([{{[(())]}}])"))
28+
println(!isValid("{{[]()}}}}"))
29+
println(!isValid("{{[](A}}}}"))
30+
println(!isValid("{[(])}"))
31+
}
32+
33+
def isValid(s: String): Boolean = {
34+
stackParenthesisSolution(s) // Stack Solution
35+
//parenthesesAreBalanced(s) // Tail recursion solution
36+
}
37+
38+
import scala.collection.mutable.Stack
39+
/**
40+
* Stack solution
41+
*/
42+
def stackParenthesisSolution(s: String): Boolean = {
43+
val stack = new Stack[Char]()
44+
if (s.size == 0) stack.push('f')
45+
46+
s.foreach(char => {
47+
if (OpenToClose.contains(char)) {
48+
stack.push(char)
49+
} else {
50+
if (stack.size > 0 && OpenToClose.get(stack.top) == Some(char))
51+
stack.pop()
52+
else
53+
stack.push('f')
54+
}
55+
})
56+
57+
stack.size == 0
58+
}
59+
60+
/**
61+
* Tail recursion solution
62+
* is taken from https://www.scala-algorithms.com/ParenthesesTailRecursive/
63+
*
64+
* @param
65+
* @return
66+
*/
67+
def parenthesesAreBalanced(s: String): Boolean = {
68+
if (s.isEmpty) true
69+
else {
70+
@scala.annotation.tailrec
71+
def go(position: Int, stack: List[Char]): Boolean = {
72+
if (position == s.length) stack.isEmpty
73+
else {
74+
val char = s(position)
75+
val isOpening = OpenToClose.contains(char)
76+
val isClosing = CloseToOpen.contains(char)
77+
if (isOpening) go(position + 1, char :: stack)
78+
else if (isClosing) {
79+
val expectedCharForMatching = CloseToOpen(char)
80+
stack match {
81+
case `expectedCharForMatching` :: rest =>
82+
go(position + 1, rest)
83+
case _ =>
84+
false
85+
}
86+
} else false
87+
}
88+
}
89+
90+
go(position = 0, stack = List.empty)
91+
}
92+
}
93+
}

0 commit comments

Comments
 (0)