Skip to content

Commit 0fae74b

Browse files
authored
Create Longest Common Prefix - Leetcode 14.py
1 parent 411bfc2 commit 0fae74b

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def longestCommonPrefix(self, strs: List[str]) -> str:
3+
min_length = float('inf')
4+
5+
for s in strs:
6+
if len(s) < min_length:
7+
min_length = len(s)
8+
9+
while i < min_length:
10+
for s in strs:
11+
if s[i] != strs[0][i]:
12+
return s[:i]
13+
i += 1
14+
return strs[0][:i]
15+
# Time: O(n * m) where n is the number of strings, m is the min word length
16+
# Space: O(1)

0 commit comments

Comments
 (0)