-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatch.js
33 lines (29 loc) · 839 Bytes
/
match.js
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
/**
* 括号匹配问题
*/
const Stack = require('./index');
function mactching (str) {
let stack = new Stack;
let rules = {
'(': ')',
'[': ']'
};
let output = str
.split('')
.some( item => {
if (item === '[' || item == '(') stack.push(item);
else {
if (stack.getLength() < 1) {
return true;
}
let left = stack.pop();
if (rules[left] != item) {
return true;
}
}
return false;
})
return !output;
}
console.log(mactching('([]())')); // => true
console.log(mactching('([())')); // => false