-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy path0014. Longest Common Prefix.py
49 lines (44 loc) · 1.46 KB
/
0014. Longest Common Prefix.py
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
49
# 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:
#
# 1 <= strs.length <= 200
# 0 <= strs[i].length <= 200
# strs[i] consists of only lower-case English letters.
# 1) Vertical scanning
# Time O(S), where S is the sum of all characters in all strings
# In the worst case there will be nn equal strings with length m and the algorithm performs S = m * n character comparisons
# In the best case there are at most n * minLen comparisons where minLen is the length of the shortest string in the array
# Space O(1)
class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
if not strs:
return ""
str0 = strs[0]
for i, c in enumerate(str0):
for s in strs[1:]:
if i >= len(s) or c != s[i]:
return str0[:i]
return str0
# 2) Vertical scanning, similar to 1)
class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
if not strs:
return ""
min_s = min(strs, key=len)
for i, c in enumerate(min_s):
for s in strs:
if c != s[i]:
return min_s[:i]
return min_s