-
Notifications
You must be signed in to change notification settings - Fork 0
/
longest-common-prefix.py
40 lines (33 loc) · 1.42 KB
/
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
# 19th Dec 2020
# Leetcode
'''
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 "".
''''
class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
'''
logic: Compare each letter of the first word with corresponding letters of all subsequent words.
IMPORTANT:
- if strs is empty then strs[0] will give an Indexerror
- if strs has only one string (empty or otherwise), then return whole word
- there exist cases like [""], ["", ""]...
'''
# if not strs:
# return ""
# elif len(strs) == 1:
# return strs[0]
# elif len(strs) > 1:
# for i in range(len(strs[0])):
# char = strs[0][i] # The problem is when the strings are empty, when stored as a char they get stored as a null object. While if we just compare (as in the other solution), it works fine
# for j in range(1, len(strs)):
# if char != strs[j][i]:
# return strs[0][0:i] # consider answer as soon as first inequality encountered
if not strs: return ""
if len(strs) == 1: return strs[0]
strs.sort()
p = ""
for x, y in zip(strs[0], strs[-1]):
if x == y: p+=x
else: break
return p