Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.55 KB | None | 0 0
  1. class Solution:
  2.     def longestCommonPrefix(self, strs: List[str]) -> str:
  3.         prefix = ""
  4.         if strs and len(strs) > 1:
  5.             sorted_list = sorted(strs)
  6.             first = sorted_list[0]
  7.             last = sorted_list[-1]
  8.             for i in range(len(first)):
  9.                 if len(last) > i and last[i] == first[i]:
  10.                     prefix += last[i]
  11.                 else:
  12.                     return prefix
  13.         elif len(strs) == 1:
  14.             return strs[0]
  15.         else:
  16.             return ""
  17.         return prefix
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement