#-*-Coding:utf8-*-
‘‘‘
__author__ = ' [email protected] '
https://oj.leetcode.com/problems/longest-common-prefix/
14:longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.
===comments by dabay===
Note the boundary condition, if STRs is empty, returns an empty string directly.
Initializes the common prefix to an empty string.
If a character appears at the corresponding position of each character, the word is multibyte to the common prefix.
‘‘‘
Class Solution:
# @return A string
def longestcommonprefix (self, STRs):
If Len (strs) = = 0:
Return ""
Common_pre = ""
i = 0
While True:
To_compare = ""
For S in STRs:
If I >= Len (s):
Return Common_pre
if To_compare = = "":
To_compare = S[i]
Continue
If s[i]! = To_compare:
Return Common_pre
Else
Common_pre = Common_pre + to_compare
i = i + 1
def main ():
s = solution ()
STRs = ["abcdef", "abc", "ABCD"]
Print S.longestcommonprefix (STRs)
if __name__ = = "__main__":
Import time
Start = Time.clock ()
Main ()
Print "%s sec"% (Time.clock ()-start)
[Leetcode] [Python]14:longest Common Prefix