Find the opening and closing element?
-
I need to find a balance between the opening and closing element, in general I wrote the code, but the problem is that I can receive the same opening and closing element, how can I check it, it turns out that both the opening and closing elements go to the stack.
For example, what comes as a string argument '| () |' or '111115611111111222288888822225577877778775555666677777777776622222' '
вот даны пары : const config1 = [['(', ')']]; const config2 = [['(', ')'], ['[', ']']]; const config3 = [['(', ')'], ['[', ']'], ['{', '}']]; const config4 = [['|', '|']]; const config5 = [['(', ')'], ['|', '|']]; const config6 = [['1', '2'], ['3', '4'], ['5', '6'], ['7', '7'], ['8', '8']]; const config7 = [['(', ')'], ['[', ']'], ['{', '}'], ['|', '|']]; сам мой код : function check(arr, bracketsConfig) { let strArr = bracketsConfig; let open = []; let close = []; let stack = []; for (var i = 0;i<strArr.length;i++){ open.push(strArr[i][0]) close.push(strArr[i][1]) } for (let j = 0; j<arr.length; j++){ if(open.includes(arr[j])&& !close.includes(arr[j]) ){ stack.push(arr[j]) }else{ if(close.indexOf(arr[j]) === open.indexOf( stack[stack.length-1] )){ let p = stack.pop() console.log(close.includes(p)) }else{ return false } } } return stack.length === 0 }
JavaScript Harrison Perry, May 28, 2019
0 Answers
Your Answer
To place the code, please use CodePen or similar tool. Thanks you!