-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlongestCommonPrefix.py
More file actions
37 lines (31 loc) · 1015 Bytes
/
longestCommonPrefix.py
File metadata and controls
37 lines (31 loc) · 1015 Bytes
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
def longestCommonPrefix(strs: list[str]) -> str:
if len(strs) == 1:
return strs[0]
elif strs[0] == "":
return ""
prefixIndex = 1
commonPrefix = ""
tempPrefix = strs[0][0:prefixIndex]
while len(tempPrefix) <= len(strs[0]):
tempPrefix = strs[0][0:prefixIndex]
for i in range(1, len(strs)):
if strs[i][0:prefixIndex] == tempPrefix:
continue
else:
return commonPrefix
commonPrefix += strs[0][prefixIndex-1]
prefixIndex += 1
if len(tempPrefix) == len(strs[0]):
break
return commonPrefix
def commonPrefix(strs: list[str]) -> str:
res = ""
for char in range(len(strs[0])): # we want to end the loop early
for word in strs:
if word[char] != strs[0][char]:
return res
res += strs[0][char]
return res
myList = ["flower", "flow", "flight"]
j = commonPrefix(myList)
print(j)