Last active
May 11, 2017 20:45
-
-
Save mlms13/cccc71ecf84496d4274a4bce2717b27f to your computer and use it in GitHub Desktop.
Balanced Brackets
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
function match(arr: Array<ParenToken>, leftCount = 0): Bool { | |
return switch arr.getOption(0) { | |
case None: leftCount == 0; | |
case Some(LeftParen): match(arr.tail(), leftCount + 1); | |
case Some(RightParen): leftCount <= 0 ? false : match(arr.tail(), leftCount - 1); | |
}; | |
} | |
function tokenize(str: String): Array<ParenToken> { | |
return str.split("").reduce(function (acc, curr) { | |
return switch curr { | |
case "(": acc.append(LeftParen); | |
case ")": acc.append(RightParen); | |
case _: acc; | |
}; | |
}, []); | |
} | |
enum ParenToken { | |
LeftParen; | |
RightParen; | |
} |
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
function match(arr, leftCount = 0) { | |
if (arr[0] == null) { | |
return leftCount == 0; | |
} else { | |
switch (arr[0]) { | |
case "(": return match(arr.slice(1), leftCount + 1); | |
case ")": return leftCount <= 0 ? false : match(arr.slice(1), leftCount - 1); | |
default: return match(arr.slice(1), leftCount); | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment