-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathvalid-parentheses.js
More file actions
46 lines (41 loc) · 954 Bytes
/
Copy pathvalid-parentheses.js
File metadata and controls
46 lines (41 loc) · 954 Bytes
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
/**
* @param {string} s
* @return {boolean}
*/
var isValid = function(s) {
var stack = new Stack();
for (var i = 0, j = s.length; i < j; i++) {
if (s[i] === '(') {
stack.push(')');
} else if (s[i] === '{') {
stack.push('}')
} else if (s[i] === '[') {
stack.push(']');
} else if (stack.isEmpty() || stack.pop() !== s[i]) {
return false;
}
}
if (!stack.isEmpty()) {
return false;
}
return true;
};
function Stack() {
this.stack = [];
};
Stack.prototype.push = function(item) {
this.stack.push(item);
}
Stack.prototype.pop = function() {
var length = this.stack.length;
if (length > 0) {
var temp = this.stack[length - 1];
this.stack.splice(length - 1, 1);
return temp;
} else {
return false;
}
}
Stack.prototype.isEmpty = function() {
return this.stack.length <= 0;
}