Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 9c0f1f4

Browse files
committed
longest common prefix algorithm
1 parent 7f2e835 commit 9c0f1f4

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
def longestCommonPrefix(self, strs: List[str]) -> str:
3+
# traverse smallest word in list
4+
# traverse all words in list
5+
# if letter in word does not equal the current letter of smallest word
6+
# return current word sliced up to the current index of smallest word
7+
if not strs:
8+
return ''
9+
10+
smallest = min(strs, key=len)
11+
for index, char in enumerate(smallest):
12+
for word in strs:
13+
if word[index] != char:
14+
return smallest[:index]
15+
16+
# finally return smallest
17+
return smallest

0 commit comments

Comments
 (0)