-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrackets.js
More file actions
47 lines (40 loc) · 1021 Bytes
/
brackets.js
File metadata and controls
47 lines (40 loc) · 1021 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
46
47
process.stdin.resume()
process.stdin.setEncoding('ascii')
let input_stdin = ''
let input_stdin_array = ''
let input_currentline = 0
process.stdin.on('data', function (data) {
input_stdin += data
})
process.stdin.on('end', function () {
input_stdin_array = input_stdin.split('\n')
main()
})
function readLine () {
return input_stdin_array[input_currentline++]
}
/// //////////// ignore above this line ////////////////////
function main () {
let t = parseInt(readLine())
for (let a0 = 0; a0 < t; a0++) {
let expression = readLine()
console.log(balancedBrackets(expression))
}
}
function balancedBrackets (expression) {
let brackets = expression.split('')
const open = []
for (const b of brackets) {
if (b === '{' || b === '[' || b === '(') {
open.push(b)
} else {
let o = open.pop()
if (b === '}' && o !== '{' ||
b === ']' && o !== '[' ||
b === ')' && o !== '(') {
return 'NO'
}
}
}
return open.length ? 'NO' : 'YES'
}