Skip to content

Instantly share code, notes, and snippets.

@mlms13
Last active May 11, 2017 20:45
Show Gist options
  • Save mlms13/cccc71ecf84496d4274a4bce2717b27f to your computer and use it in GitHub Desktop.
Save mlms13/cccc71ecf84496d4274a4bce2717b27f to your computer and use it in GitHub Desktop.
Balanced Brackets
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;
}
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