File tree Expand file tree Collapse file tree 2 files changed +40
-0
lines changed Expand file tree Collapse file tree 2 files changed +40
-0
lines changed Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments