14. Longest Common Prefix

Write a function to find the longest common prefix string amongst an array of strings.

Solution:

class Solution(object):
    def longestCommonPrefix(self, strs):
        """
        :type strs: List[str]
        :rtype: str
        """
        prefix = []
        min_len = min(map(len, strs)) if strs else 0
        for idx in xrange(min_len):
            char = strs[0][idx]
            if any(s[idx] != char for s in strs):
                break
            prefix.append(char)
        return ''.join(prefix)

results matching ""

    No results matching ""