kolesoffac

remove brackets

Mar 28th, 2018
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Andrey solution
  2. //function test(s) {
  3. //  let depth = 0
  4. //  for (let char of s) {
  5. //      if (char === '(') depth++
  6. //    if (char === ')') depth--
  7. //    if (depth < 0) return false
  8. //  }
  9. //  return depth === 0
  10. //}
  11.  
  12. function test(s) {
  13. var str = s;
  14.  
  15. while (str.includes("(") && str.includes(")") && str.indexOf("(") < str.indexOf(")")  ) {
  16. str = str.replace("(", "");
  17. str = str.split("").reverse().join("");
  18. str = str.replace(")", "");
  19. str = str.split("").reverse().join("");
  20.  
  21. }
  22.  
  23. return !(str.includes("(") || str.includes(")"))
  24. }
  25.  
  26.  
  27.  
  28. alert(test('()')) // true
  29. alert(test(')('))
  30. alert(test('(')) // false
  31. alert(test('qwe(qwrtqer(tqertqe)rtqertqet)qwer'))// true
  32. alert(test('__)__(__(___)___)__')) // false
Advertisement
Add Comment
Please, Sign In to add comment