Advertisement
Guest User

Untitled

a guest
Jun 18th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. /**
  2. * @param {string} s
  3. * @return {boolean}
  4. */
  5. var isValid = function(s) {
  6. let x = s.length
  7. let y = s.split("");
  8. let stack = []
  9. if(s == ""){
  10. return true;
  11. }
  12. if(x == 1){
  13. return false;
  14. }
  15. for(let i =0; i < x; i++){
  16. if(y[i] == '{' || y[i] == '(' || y[i] == '['){
  17. stack.push(y[i]);
  18. }
  19. else{
  20. if(y[i]=="}"){
  21. if(stack[stack.length -1] == '{'){
  22. stack.pop();
  23. }
  24. else{
  25. return false;
  26. }
  27. }
  28. else if(y[i] == "]"){
  29. if(stack[stack.length -1] == '['){
  30. stack.pop();
  31. }
  32. else{
  33. return false;
  34. }
  35. }
  36. else if(y[i] == ")"){
  37. if(stack[stack.length -1] == '('){
  38. stack.pop();
  39. }
  40. else{
  41. return false;
  42. }
  43. }
  44. else{
  45. return false;
  46. }
  47. }
  48. }
  49. return stack.length == 0;
  50. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement