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: strs = [“flower”,”flow”,”flight”]
Output: “fl”
Example 2:
Input: strs = [“dog”,”racecar”,”car”]
Output: “”
Explanation: There is no common prefix among the input strings.
Constraints:
1 <= strs.length <= 200
0 <= strs[i].length <= 200
*
strs[i]
consists of only lower-case English letters.题目大意:
字符串列表的最长前缀
算法思路:
N/A
注意事项:
- 求最小值len初始值用最大值而不是0
- char = strs[0][i]而不是char = strs[i]
Python代码:
1 | def longestCommonPrefix(self, strs: List[str]) -> str: |
算法分析:
时间复杂度为O(nm)
,空间复杂度O(1)