Question: http://oj.leetcode.com/problems/longest-common-prefix/
Question Name: Longest Common Prefix
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | class Solution: # @return a string def longestCommonPrefix(self, strs): if len(strs) == 0: return "" longest = len(strs[0]) # use the first string as the base for string in strs[1:]: # Compute the new longest common prefix of current string # and longest common prefix so far from previous rounds index = 0 while index < len(string) and index < longest and strs[0][index] == string[index]: index += 1 longest = min(index, longest) return strs[0][:longest] |