We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 125c4d8 commit bccfc6cCopy full SHA for bccfc6c
algorithms/python/LongestCommonPrefix/longestCommonPrefix.py
@@ -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
12
13
+ return prefix
14
15
+if __name__ == "__main__":
16
+ strs = ["abab", "aba", "abc"]
17
+ print(longest_common_prefix(strs))
0 commit comments