Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
a-abdellatif98 committed Dec 28, 2021
0 parents commit 7bdcf9a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Longest Common Prefix.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
def longest_common_prefix(strs)
return "" if strs.nil? || strs.size == 0

return strs[0] if strs.size == 1

output = ""

for i in 0...strs[0].size do
for j in 1...strs.size do
if strs[j][i] != strs[0][i]
output = strs[0][0...i]
return output
end
end
output << strs[0][i]
end

output

end
20 changes: 20 additions & 0 deletions valid-parentheses.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# @param {String} s
# @return {Boolean}
def is_valid(s)
return true if s.empty?

stack = []
s.each_char do |c|
case c
when '(', '{', '['
stack.push(c)
when ')'
return false if stack.pop() != '('
when '}'
return false if stack.pop() != '{'
when ']'
return false if stack.pop() != '['
end
end
return stack.empty?
end

0 comments on commit 7bdcf9a

Please sign in to comment.