Skip to content

Instantly share code, notes, and snippets.

@dudo
Last active December 3, 2019 17:12
Show Gist options
  • Select an option

  • Save dudo/65720e0b781f2bb2b72d7b40b9aa9be1 to your computer and use it in GitHub Desktop.

Select an option

Save dudo/65720e0b781f2bb2b72d7b40b9aa9be1 to your computer and use it in GitHub Desktop.
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
@dudo
Copy link
Author

dudo commented Dec 3, 2019

LIFO (Last In First Out) example, commonly known as a stack.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment