LyWang

longest_common_prefix

Nov 18th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.56 KB | None | 0 0
  1. class Solution:
  2.     """
  3.    @param strs: A list of strings
  4.    @return: The longest common prefix
  5.    """
  6.     def longestCommonPrefix(self, strs):
  7.         # write your code here
  8.         if len(strs)==0:
  9.             return ""
  10.         for prefix in range(len(strs[0])):
  11.             mark = strs[0][prefix]
  12.             for word in strs[1:]:
  13.                 if prefix < len(word):
  14.                     if word[prefix] != mark:
  15.                         return strs[0][:prefix]
  16.                 else:
  17.                     return strs[0][:prefix]
  18.         return strs[0]
Add Comment
Please, Sign In to add comment