14. Longest Common Prefix leetcode Python 2016 new Season

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

to compare all the strings assume the longest_prefix's length is m and there are n strings. we will need at least o(mn) time. 

class Solution(object):
    def longestCommonPrefix(self, strs):
        """
        :type strs: List[str]
        :rtype: str
        """
        if not strs:
            return ''
        longest_string = min(strs)
        for index in range(len(longest_string)):
            for string in strs:
                if longest_string[index] != string[index]:
                    return longest_string[:index] if index > 0 else ''
        return longest_string


你可能感兴趣的:(14. Longest Common Prefix leetcode Python 2016 new Season)