> For the complete documentation index, see [llms.txt](https://nataliekung.gitbook.io/ladder_code/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://nataliekung.gitbook.io/ladder_code/ms/longest-common-prefix.md).

# Longest Common Prefix

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.
```

分析

按照长度排序 sort(key=len),取第一个str和后面所有str 按照index一个个比，第一个不等的就是最短长度

```
class Solution:
    def longestCommonPrefix(self, strs: List[str]) -> str:
        if not strs:
            return ''
        strs.sort(key=len)
        cur = strs[0]
        for i,s in enumerate(cur):
            for others in strs[1:]:
                if others[i]!=s:
                    return cur[:i]
        return cur
            
        
```
