Skip to content

Commit 0e4f184

Browse files
committed
valid-parentheses
1 parent 12ec55e commit 0e4f184

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

valid-parentheses/taewanseoul.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* 20. Valid Parentheses
3+
* Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
4+
*
5+
* An input string is valid if:
6+
* 1. Open brackets must be closed by the same type of brackets.
7+
* 2. Open brackets must be closed in the correct order.
8+
* 3. Every close bracket has a corresponding open bracket of the same type.
9+
*
10+
* https://leetcode.com/problems/valid-parentheses/
11+
*
12+
*/
13+
14+
// O(n^2) time
15+
// O(1) space
16+
function isValid(s: string): boolean {
17+
if (s.length % 2 === 1) return false;
18+
19+
while (
20+
s.indexOf("()") !== -1 ||
21+
s.indexOf("{}") !== -1 ||
22+
s.indexOf("[]") !== -1
23+
) {
24+
s = s.replace("()", "");
25+
s = s.replace("{}", "");
26+
s = s.replace("[]", "");
27+
}
28+
29+
return s === "";
30+
}

0 commit comments

Comments
 (0)