Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @param {string} s
- * @return {boolean}
- */
- var isValid = function(s) {
- let x = s.length
- let y = s.split("");
- let stack = []
- if(s == ""){
- return true;
- }
- if(x == 1){
- return false;
- }
- for(let i =0; i < x; i++){
- if(y[i] == '{' || y[i] == '(' || y[i] == '['){
- stack.push(y[i]);
- }
- else{
- if(y[i]=="}"){
- if(stack[stack.length -1] == '{'){
- stack.pop();
- }
- else{
- return false;
- }
- }
- else if(y[i] == "]"){
- if(stack[stack.length -1] == '['){
- stack.pop();
- }
- else{
- return false;
- }
- }
- else if(y[i] == ")"){
- if(stack[stack.length -1] == '('){
- stack.pop();
- }
- else{
- return false;
- }
- }
- else{
- return false;
- }
- }
- }
- return stack.length == 0;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement