forked from mohitjain/leetcode_solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path14_longest_common_prefix.rb
More file actions
48 lines (44 loc) · 1.15 KB
/
14_longest_common_prefix.rb
File metadata and controls
48 lines (44 loc) · 1.15 KB
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
38
39
40
41
42
43
44
45
46
47
48
# Leetcode Problem: https://leetcode.com/problems/longest-common-prefix/
#Write a function to find the longest common prefix string amongst an array of strings.
#
# If there is no common prefix, return an empty string "".
#
#
#
# Example 1:
#
# Input: strs = ["flower","flow","flight"]
# Output: "fl"
# Example 2:
#
# Input: strs = ["dog","racecar","car"]
# Output: ""
# Explanation: There is no common prefix among the input strings.
#
#
# Constraints:
#
# 0 <= strs.length <= 200
# 0 <= strs[i].length <= 200
# strs[i] consists of only lower-case English letters.
# ----------------------------------------------------------------------------------------------------------------------
# @param {String[]} strs
# @return {String}
def longest_common_prefix(strs)
return '' if strs.empty?
prefix = ''
s = strs[0]
s.length.times.each do |index|
char = s[index]
all_has = true
strs.each do |str|
all_has &&= str.start_with?(prefix + char)
end
return prefix unless all_has
prefix += char
end
prefix
end
p longest_common_prefix %w[flower flow flight]
p longest_common_prefix %w[dog racecar car]
p longest_common_prefix %w[aaa aa aaa]