-
Notifications
You must be signed in to change notification settings - Fork 0
/
longest-common-prefix_14.py
54 lines (40 loc) · 1.49 KB
/
longest-common-prefix_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
# 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.
# ---------------------------------------Runtime 64 ms Beats 5.2% Memory 16.3 MB Beats 19.4%---------------------------------------
from typing import List
# My solution
class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
i = el = 0
pattern = ""
while i <= len(strs[0]):
i += 1
temp_pattern = strs[0][:i]
while el <= len(strs) - 1:
if temp_pattern == strs[el][:i] and el == len(strs) - 1:
pattern = temp_pattern
elif temp_pattern != strs[el][:i]:
return pattern
el += 1
el = 0
return pattern
# ---------------------------------------Runtime 57 ms Beats 7.43% Memory 16.4 MB Beats 19.4%---------------------------------------
# Solution @abdullayevakbar0101
class Solution:
def longestCommonPrefix(self, v: List[str]) -> str:
ans = ""
v = sorted(v)
first = v[0]
last = v[-1]
for i in range(min(len(first), len(last))):
if first[i] != last[i]:
return ans
ans += first[i]
return ans