Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Andrey solution
- //function test(s) {
- // let depth = 0
- // for (let char of s) {
- // if (char === '(') depth++
- // if (char === ')') depth--
- // if (depth < 0) return false
- // }
- // return depth === 0
- //}
- function test(s) {
- var str = s;
- while (str.includes("(") && str.includes(")") && str.indexOf("(") < str.indexOf(")") ) {
- str = str.replace("(", "");
- str = str.split("").reverse().join("");
- str = str.replace(")", "");
- str = str.split("").reverse().join("");
- }
- return !(str.includes("(") || str.includes(")"))
- }
- alert(test('()')) // true
- alert(test(')('))
- alert(test('(')) // false
- alert(test('qwe(qwrtqer(tqertqe)rtqertqet)qwer'))// true
- alert(test('__)__(__(___)___)__')) // false
Advertisement
Add Comment
Please, Sign In to add comment