-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidParentheses.js
More file actions
63 lines (57 loc) · 1.64 KB
/
Copy pathValidParentheses.js
File metadata and controls
63 lines (57 loc) · 1.64 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
* @fileoverview
* Valid Parentheses
* (LeetCode Easy: 20. Valid Parentheses)
*
* Target: Given a string containing '(', ')', '{', '}', '[' and ']',
* determine if the input string is valid.
*/
/**
* Strategy: Use a Stack
* 1. Initialize an empty stack.
* 2. Traverse the string char by char.
* 3. If it's an opening bracket, push it to the stack.
* 4. If it's a closing bracket, check if the top of the stack matches.
* - If match, pop and continue.
* - If no match (or stack empty), return false.
* 5. After the loop, if stack is empty, return true.
*
* @param {string} s
* @return {boolean}
*/
function isValid(s) {
const stack = [];
const map = {
')': '(',
'}': '{',
']': '['
};
for (let char of s) {
if (char === '(' || char === '{' || char === '[') {
stack.push(char);
} else {
// It's a closing bracket
const top = stack.pop();
if (top !== map[char]) {
return false;
}
}
}
return stack.length === 0;
}
/**
* 📈 Complexity Analysis:
* -----------------------
* Time Complexity: O(N) - We traverse the string exactly once.
* Space Complexity: O(N) - In the worst case (all opening brackets),
* the stack grows to N.
*/
// ------------------------------------
// 🧪 Test Cases
// ------------------------------------
console.log("()[]{} :", isValid("()[]{}")); // true
console.log("(] :", isValid("(]")); // false
console.log("([)] :", isValid("([)]")); // false
console.log("{[]} :", isValid("{[]}")); // true
console.log("( :", isValid("(")); // false
console.log(") :", isValid(")")); // false