-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathproblem_14.py
55 lines (52 loc) · 1.77 KB
/
problem_14.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
50
51
52
53
54
55
# Problem 14. Longest Common Prefix (Easy): https://leetcode.com/problems/longest-common-prefix/
class Solution:
def longestCommonPrefix(self, strs):
matched = True
result = ""
index = 0
k = 0
smallestLength = 200
for i in range(len(strs)):
if(len(strs[i]) < smallestLength):
smallestLength = len(strs[i])
k = i
if(smallestLength == 0):
return result
while(matched):
toMatch = strs[k][index]
for i in range(len(strs)):
if(index < len(strs[i]) and strs[i][index] != toMatch):
matched = False
return result
result += toMatch
index += 1
if(index >= smallestLength):
break
return result
class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
strs.sort()
print(strs)
result = ""
first = strs[0]
last = strs[-1]
for i in range(min(len(first), len(last))):
if(first[i] != last[i]):
return result
result += first[i]
return result
if __name__ == "__main__":
strs = ["flower","flow","flight"]
print(Solution().longestCommonPrefix(strs)) # "fl"
strs = ["dog","racecar","car"]
print(Solution().longestCommonPrefix(strs)) # ""
strs = ["a"]
print(Solution().longestCommonPrefix(strs)) # "a"
strs = ["a","a"]
print(Solution().longestCommonPrefix(strs)) # "a"
strs = ["a","b"]
print(Solution().longestCommonPrefix(strs)) # ""
strs = ["a","ab"]
print(Solution().longestCommonPrefix(strs)) # "a"
strs = ["flowr","flowr","flow","flowesr"]
print(Solution().longestCommonPrefix(strs)) # "flow"