Skip to content

Commit bcbd9e5

Browse files
committed
valid-parentheses
1 parent d2bc9df commit bcbd9e5

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

valid-parentheses/taewanseoul.ts

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

0 commit comments

Comments
 (0)