diff --git a/LeetCode/LongestCommonPrefix.py b/LeetCode/LongestCommonPrefix.py new file mode 100644 index 0000000..9d7620a --- /dev/null +++ b/LeetCode/LongestCommonPrefix.py @@ -0,0 +1,15 @@ +class Solution(object): + def longestCommonPrefix(self, strs): + """ + :type strs: List[str] + :rtype: str + """ + maximum_common_length = min(len(x) for x in strs) + + common_str = "" + for i in range(maximum_common_length): + if all(x[i] == strs[0][i] for x in strs): + common_str += strs[0][i] + else: + break + return common_str diff --git a/LeetCode/README.md b/LeetCode/README.md index 6699c6f..42e2888 100644 --- a/LeetCode/README.md +++ b/LeetCode/README.md @@ -11,6 +11,7 @@ | [Integer to Roman](IntegerToRoman.py) | :heavy_check_mark: | `#string` `#math` | Python | | [Invert Binary Tree](InvertBinaryTree.py) | :heavy_check_mark: | `#tree` | Python | | [K-th Symbol in Grammar](KthSymbolInGrammar.py) | :heavy_check_mark: | `#recursion` | Python | +| [Longest Common Prefix](LongestCommonPrefix.py) | :heavy_check_mark: | `#trie` `#string` | Python | | [Longest Substring without repeating characters](LongestSubstrWithoutRepeatingChars.py) | :heavy_check_mark: | `#hash table` `#two pointers` `#string` `#sliding window` | Python | | [Maximum Depth Of Binary Tree](MaximumDepthOfBinaryTree.py) | :heavy_check_mark: | `#recursion` `#tree` | Python | | [Maximum Subarray](MaximumSubarray.py) | :heavy_check_mark: | `#kadane` `#array` | Python |