Friday, September 12, 2014

Leetcode: Longest Common Prefix @Python

class Solution:  
    # @return a string  
    def longestCommonPrefix(self, strs):  
        if len(strs) == 0:  
            return ""  
          
        for i in range(len(strs[0])):  
            for j in range(len(strs)):  
                if len(strs[j]) <= i or strs[j][i] != strs[0][i]:  
                    return (strs[0])[0 : i]                  
        return strs[0]  

No comments :

Post a Comment