From bccfc6c401fc9e1957b5242d7b8b2f997bc2ab5d Mon Sep 17 00:00:00 2001 From: "dmitry.a" Date: Wed, 5 Feb 2025 12:11:12 +0300 Subject: [PATCH 1/2] longest common prefix python --- .../LongestCommonPrefix/longestCommonPrefix.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 algorithms/python/LongestCommonPrefix/longestCommonPrefix.py diff --git a/algorithms/python/LongestCommonPrefix/longestCommonPrefix.py b/algorithms/python/LongestCommonPrefix/longestCommonPrefix.py new file mode 100644 index 00000000..fbf7fd3e --- /dev/null +++ b/algorithms/python/LongestCommonPrefix/longestCommonPrefix.py @@ -0,0 +1,17 @@ +def longest_common_prefix(strs): + if not strs: + return "" + + prefix = strs[0] + + for s in strs[1:]: + while s[:len(prefix)] != prefix and prefix: + prefix = prefix[:-1] + if not prefix: + return "" + + return prefix + +if __name__ == "__main__": + strs = ["abab", "aba", "abc"] + print(longest_common_prefix(strs)) \ No newline at end of file From ae37c6cb1fe419dcb4511895fc6dd6070f7c99b2 Mon Sep 17 00:00:00 2001 From: "dmitry.a" Date: Wed, 5 Feb 2025 12:12:53 +0300 Subject: [PATCH 2/2] update README.md --- README.md | 2 +- algorithms/python/LongestCommonPrefix/longestCommonPrefix.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2bf93af3..b4fe66ed 100644 --- a/README.md +++ b/README.md @@ -548,7 +548,7 @@ LeetCode |17|[Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/)| [C++](./algorithms/cpp/letterCombinationsOfAPhoneNumber/letterCombinationsOfAPhoneNumber.cpp)|Medium| |16|[3Sum Closest](https://leetcode.com/problems/3sum-closest/)| [C++](./algorithms/cpp/3SumClosest/3SumClosest.cpp)|Medium| |15|[3Sum](https://leetcode.com/problems/3sum/)| [C++](./algorithms/cpp/3Sum/3Sum.cpp)|Medium| -|14|[Longest Common Prefix](https://leetcode.com/problems/longest-common-prefix/)| [C++](./algorithms/cpp/longestCommonPrefix/longestCommonPrefix.cpp)|Easy| +|14|[Longest Common Prefix](https://leetcode.com/problems/longest-common-prefix/)| [C++](./algorithms/cpp/longestCommonPrefix/longestCommonPrefix.cpp) [Python](./algorithms/python/longestCommonPrefix/longestCommonPrefix.py)|Easy| |13|[Roman to Integer](https://leetcode.com/problems/roman-to-integer/)| [C++](./algorithms/cpp/romanToInteger/romanToInteger.cpp)|Easy| |12|[Integer to Roman](https://leetcode.com/problems/integer-to-roman/)| [C++](./algorithms/cpp/integerToRoman/integerToRoman.cpp)|Medium| |11|[Container With Most Water](https://leetcode.com/problems/container-with-most-water/)| [C++](./algorithms/cpp/containerWithMostWater/containerWithMostWater.cpp), [Java](./algorithms/java/src/containerwithmostwater.java)|Medium| diff --git a/algorithms/python/LongestCommonPrefix/longestCommonPrefix.py b/algorithms/python/LongestCommonPrefix/longestCommonPrefix.py index fbf7fd3e..5ff4e9a5 100644 --- a/algorithms/python/LongestCommonPrefix/longestCommonPrefix.py +++ b/algorithms/python/LongestCommonPrefix/longestCommonPrefix.py @@ -14,4 +14,4 @@ def longest_common_prefix(strs): if __name__ == "__main__": strs = ["abab", "aba", "abc"] - print(longest_common_prefix(strs)) \ No newline at end of file + print(longest_common_prefix(strs))