Created
February 28, 2019 09:32
-
-
Save piyusharora1989/75665354043c3b06b1d83ab7580efaaa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// '[]{}()' - allowed characters | |
// Assusming that only above characters are used. Write a code to validate the given pattern. | |
// '[{[()]}]' - valid | |
// '[{[()]}]{[{()}]}' - valid | |
// '[()[]]' | |
// '[{[()]]}]' - not valid | |
// '[}' - not valid | |
// '[{]}' - not valid | |
// '[]{}' - valid | |
let allowedOpenChar = '[{('.split(); | |
let allowedClosedChar = ']})'.split(); | |
function findvalidSeq(seq) { | |
let seqArr = seq.split(''); | |
let startSeq = {}; | |
let endSeq = {}; | |
for (let i = 0; i < seqArr.length; i++){ | |
if(seqArr[i] == '[' || eqArr[i] == '{' || eqArr[i] == '(' ){ | |
startSeq[seqArr[i]] = startSeq[seqArr[i]]?startSeq[seqArr[i]]:[]; | |
startSeq[seqArr[i]].push(i); | |
} else { | |
endSeq[seqArr[i]] = endSeq[seqArr[i]]?endSeq[seqArr[i]]: []; | |
endSeq[seqArr[i]].push(i); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment