Skip to content

Instantly share code, notes, and snippets.

@piyusharora1989
Created February 28, 2019 09:32
Show Gist options
  • Save piyusharora1989/75665354043c3b06b1d83ab7580efaaa to your computer and use it in GitHub Desktop.
Save piyusharora1989/75665354043c3b06b1d83ab7580efaaa to your computer and use it in GitHub Desktop.
// '[]{}()' - 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