-
Notifications
You must be signed in to change notification settings - Fork 0
/
20-validParenthesis.ts
39 lines (33 loc) · 1.25 KB
/
20-validParenthesis.ts
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
// Desc.: Given a string s 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.
// Every close bracket has a corresponding open bracket of the same type.
// Expl.: Iterating through string array, while encounter (,[,{ pushing them into stack, when encounter ),],} it should be in last pos in stack, in the end, if stack is empty,
// returning true, otherwise false.
const isValid = (s: string): boolean => {
let stringArr: string[] = s.split("")
let stack: string[] = []
for (let i =0; i < stringArr.length; i++) {
if (stringArr[i] === "{" || stringArr[i] === "(" || stringArr[i] === "[" ) {
stack.push(stringArr[i])
}
if (stringArr[i] === "}" && stack.pop() !== "{") {
return false
}
if (stringArr[i] === "]" && stack.pop() !== "[") {
return false
}
if (stringArr[i] === ")" && stack.pop() !== "(") {
return false
}
}
if (stack.length > 0) {
return false
} else {
return true
}
};
console.log(isValid("()"))
console.log(isValid("()[]{}"))
console.log(isValid("(]"))