Skip to content

Instantly share code, notes, and snippets.

@gcjbr
Created January 6, 2021 18:37
Show Gist options
  • Save gcjbr/ed46bf9da31558a0adf4ce8313ba0976 to your computer and use it in GitHub Desktop.
Save gcjbr/ed46bf9da31558a0adf4ce8313ba0976 to your computer and use it in GitHub Desktop.
function SearchingChallenge(str: string): string | number {
const openers = ['(', '['];
const closers = [')', ']'];
const collection = [];
let counter: number = 0;
let isValid = true;
[...str].forEach((char)=> {
if(openers.includes(char)) {
collection.push(char);
counter++;
}
if(closers.includes(char)) {
counter--;
}
if(counter < 0) {
isValid = false;
}
})
return isValid ? `1 ${collection.length}` : 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment