title: LeetCode "Longest Common Prefix" published: true description: Solution of LeetCode "Longest Common Prefix" tags: ["leetcode", "python"]


Oh😮 I didn't know that question number is not fixed😧

Longest Common Prefix

```python class Solution: def longestCommonPrefix(self, strs: List[str]) -> str: if len(strs) == 0: return ''

    # check what is needed length
    min_len = len(strs[0])
    other_strs = strs[1:]
    for s in other_strs:
        len_s = len(s)
        if min_len > len_s:
            min_len = len_s

    # check prefix
    c = ''
    prefix = ''
    for i in range(min_len):
        c = strs[0][i]
        for s in other_strs:
            if c != s[i]:
                c = ''

        # not longer a prefix
        if not c:
            break

        prefix += c

    return prefix

```