Longest Common Prefix

LeetCode 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: strs = [“flower”,”flow”,”flight”] Output: “fl”

Example 2:

Input: strs = [“dog”,”racecar”,”car”] Output: “” Explanation: There is no common prefix among the input strings.

풀이

class Solution:
    def longestCommonPrefix(self, strs: List[str]) -> str:
        if not strs:
            return ''
        
        prefix = strs[0]
        for s in strs[1:]:
            while not s.startswith(prefix):
                prefix = prefix[:-1]
                if not prefix:
                    return ''
        return prefix
* TOC {:toc}

© 2021. All rights reserved.