Skip to content

Commit 865f01b

Browse files
committed
exercism-65 matching Brackets
1 parent 421199f commit 865f01b

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ solution of many challenges of [Leetcode](https://leetcode.com/), [Exercism](htt
282282
62. [Nth Prime](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/nth_prime.rb)
283283
63. [Perfect Numbers](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/perfect_numbers.rb)
284284
64. [Binary Search](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/binary_search.rb)
285+
65. [Matching Brackets](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/exercism/matching_brackets.rb)
285286

286287
<a name="leetcode"/>
287288

exercism/matching_brackets.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Problem: https://exercism.org/tracks/ruby/exercises/matching-brackets
2+
3+
# Solution
4+
class Brackets
5+
BRACKETS = {
6+
"(" => ")",
7+
"{" => "}",
8+
"[" => "]"
9+
}
10+
def self.paired?(input)
11+
stack = []
12+
input.chars.each do |char|
13+
if BRACKETS.keys.include?(char)
14+
stack.push(char)
15+
elsif BRACKETS.values.include?(char)
16+
char == BRACKETS[stack[-1]] ? stack.pop : (return false)
17+
end
18+
end
19+
stack.empty?
20+
end
21+
end

0 commit comments

Comments
 (0)