Skip to content

Commit 7bdcf9a

Browse files
init
0 parents  commit 7bdcf9a

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

Longest Common Prefix.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
def longest_common_prefix(strs)
2+
return "" if strs.nil? || strs.size == 0
3+
4+
return strs[0] if strs.size == 1
5+
6+
output = ""
7+
8+
for i in 0...strs[0].size do
9+
for j in 1...strs.size do
10+
if strs[j][i] != strs[0][i]
11+
output = strs[0][0...i]
12+
return output
13+
end
14+
end
15+
output << strs[0][i]
16+
end
17+
18+
output
19+
20+
end

valid-parentheses.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# @param {String} s
2+
# @return {Boolean}
3+
def is_valid(s)
4+
return true if s.empty?
5+
6+
stack = []
7+
s.each_char do |c|
8+
case c
9+
when '(', '{', '['
10+
stack.push(c)
11+
when ')'
12+
return false if stack.pop() != '('
13+
when '}'
14+
return false if stack.pop() != '{'
15+
when ']'
16+
return false if stack.pop() != '['
17+
end
18+
end
19+
return stack.empty?
20+
end

0 commit comments

Comments
 (0)