Skip to content

Commit bccfc6c

Browse files
committed
longest common prefix python
1 parent 125c4d8 commit bccfc6c

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+
def longest_common_prefix(strs):
2+
if not strs:
3+
return ""
4+
5+
prefix = strs[0]
6+
7+
for s in strs[1:]:
8+
while s[:len(prefix)] != prefix and prefix:
9+
prefix = prefix[:-1]
10+
if not prefix:
11+
return ""
12+
13+
return prefix
14+
15+
if __name__ == "__main__":
16+
strs = ["abab", "aba", "abc"]
17+
print(longest_common_prefix(strs))

0 commit comments

Comments
 (0)