- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rb
37 lines (31 loc) · 810 Bytes
/
main.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# divide and conquer
# @param {String[]} strs
# @return {String}
deflongest_common_prefix(strs)
ifstrs.nil? || strs.size <= 0
return""
end
longest_common_prefix_helperstrs,0,strs.size - 1
end
deflongest_common_prefix_helper(strs,left,right)
ifleft == right
strs[left]
else
mid=(left + right) / 2
str1=longest_common_prefix_helperstrs,left,mid
str2=longest_common_prefix_helperstrs,mid + 1,right
common_prefixstr1,str2
end
end
defcommon_prefix(str1,str2)
minimum=[str1.size,str2.size].min
result=""
0.upto(minimum)do |i|
ifstr1[i] != str2[i]
result=str1[0...i]
returnresult
end
end
result=str1[0...minimum]
result
end