-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0014.py
More file actions
48 lines (30 loc) · 935 Bytes
/
0014.py
File metadata and controls
48 lines (30 loc) · 935 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
38
39
40
41
42
43
44
45
46
# ----------------------------------------------------------------------
# 0014. Longest Common Prefix
# ----------------------------------------------------------------------
# Only lowercase Eng words
# Inputs:
strs = ["flower","flow","flight"] # ans = 'fl'
n = len(strs)
# ----------------------------------------------------------------------
# Sol1: Hash
# Time / Space = O(n^2) / O(1)
ans = ''
w = 0; i = 0; # w is word index, i is character index
m = len(strs[w]) # first word
lens(strs[:])
for i in range(201):
if len(strs[w])>0
for w in range(1,n):
ans.append()
if strs[w][i] ==
for s in strs:
if s
""
'flower' + 'fl'
print(ans)
# ----------------------------------------------------------------------
# LeetCode submit: Sol2, O(n) / O(1)
class Solution:
def longestCommonPrefix(self, strs: list[str]) -> str:
sol = Solution()
sol.longestCommonPrefix(strs)