Skip to content

Instantly share code, notes, and snippets.

@scottmries
Created November 16, 2023 20:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scottmries/37a6718265d6d4984cf1a41cc96fbab8 to your computer and use it in GitHub Desktop.
Save scottmries/37a6718265d6d4984cf1a41cc96fbab8 to your computer and use it in GitHub Desktop.
brackets problem
'()[]{}'
'(((]))'
']('
processClosedBracket('()[]{}')
processOpenBracket('paren', ')[]{}')
const isOpening = (char) => {
return char in ['(', '{', '[']
}
const isClosing = (char) => {
return char in [')', '}', ']']
}
const isStringClosed = (str) => {
if (str[0] in ['(', '{', '[']) {
return processOpenBracket(str[0], str.slice(1))
}
return false
}
const closedVersions = {
'(': ')',
'[': ']',
'{': '}',
}
'(((]))'
'(,)))'
'(,]))'
'],))'
'(])'
'(,])'
'],)'
const processOpenBracket = (type, string) => {
if isOpening(string[0]) {
return processOpenBracket(string[0], string.slice(1))
}
return closedVersions[type] === string[0]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment