Created
November 16, 2023 20:00
-
-
Save scottmries/37a6718265d6d4984cf1a41cc96fbab8 to your computer and use it in GitHub Desktop.
brackets problem
This file contains hidden or 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
'()[]{}' | |
'(((]))' | |
'](' | |
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