Last active
December 3, 2019 17:12
-
-
Save dudo/65720e0b781f2bb2b72d7b40b9aa9be1 to your computer and use it in GitHub Desktop.
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
| class String | |
| def balanced? | |
| pairs = { '{' => '}', '[' => ']', '(' => ')' } | |
| brackets = chars.each_with_object([]) do |char, stack| | |
| if bracket = pairs[char] | |
| stack << bracket | |
| elsif pairs.values.include?(char) | |
| next if stack.pop == char | |
| stack << char | |
| break stack | |
| end | |
| end | |
| brackets.empty? | |
| end | |
| end |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
LIFO (Last In First Out) example, commonly known as a stack.