-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchallenge_68.js
More file actions
53 lines (51 loc) · 1.27 KB
/
challenge_68.js
File metadata and controls
53 lines (51 loc) · 1.27 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
function isValid(s) {
var o_stk = []
, pairs = {
'(' : ["open", ")" ]
,')' : ["closed", "(" ]
,'[' : ["open", "]" ]
,']' : ["closed", "[" ]
,'{' : ["open", "}" ]
,'}' : ["closed", "{" ]
}
, o_c
, j = s.length
, i = 0
;
while ( i < j) {
//console.log("Starting iteration: " + i );
if (pairs[ s[i] ]) {
oc = pairs[ s[i] ]
//console.log(s[i] + " ==> oc: " + oc[0] + ", o_stk: " + o_stk);
if ( oc[0] === "open" ) {
//console.log("pushed '" + s[i] + "' onto o_stk");
o_stk.push( s[i] );
} else if ( oc[0] === "closed" ) {
//console.log("is closed");
if ( o_stk[o_stk.length-1] === oc[1] ) {
// matched pair found
o_stk.pop();
//console.log("o_stk popped");
i += 1;
continue;
} else {
// mismatch found - no need to
//console.log("mismatch!");
return "False";
}
}
}
i += 1;
}
if (o_stk.length === 0) {
return "True";
} else {
return "False";
}
}
var fs = require("fs");
fs.readFileSync(process.argv[2]).toString().split('\n').forEach(function (line) {
if (line !== "") { // ignore empty lines
console.log( isValid(line) );
}
});