diff --git a/0014-Longest-Common-Prefix.py b/0014-Longest-Common-Prefix.py new file mode 100644 index 0000000..e3d46f3 --- /dev/null +++ b/0014-Longest-Common-Prefix.py @@ -0,0 +1,34 @@ +''' +Write a function to find the longest common prefix string amongst an array of strings. + +If there is no common prefix, return an empty string "". + +Example 1: + +Input: ["flower","flow","flight"] +Output: "fl" + +Example 2: + +Input: ["dog","racecar","car"] +Output: "" +Explanation: There is no common prefix among the input strings. + +Note: + +All given inputs are in lowercase letters a-z. +''' +# Time Complexity: O(n) +# Space Complexity: O(1) +class Solution: + def longestCommonPrefix(self, strs: List[str]) -> str: + if not strs: + return '' + + min_len = min(strs, key = len) + + for i in range(len(min_len)): + if sum(1 for s in strs if min_len[i] != s[i]) > 0: + return min_len[:i] + + return min_len