Skip to content

Commit b0bcc23

Browse files
authored
Create longest-common-pref.py
neat enough solution for longest common prefix
1 parent 06bb242 commit b0bcc23

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

longest-common-pref.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def longestCommonPrefix(self, strs: List[str]) -> str:
3+
pref = strs[0]
4+
for s in strs:
5+
if pref == "":
6+
return pref
7+
for i in range(min(len(s),len(pref))):
8+
if s[i] != pref[i]:
9+
pref = pref[:i]
10+
break
11+
if len(pref) > len(s):
12+
pref = pref[:len(s)]
13+
return pref

0 commit comments

Comments
 (0)