Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def longestCommonPrefix(self, strs: List[str]) -> str:
- if not strs:
- return ""
- for i in range(len(strs[0])):
- char = strs[0][i]
- for s in strs:
- if i >= len(s) or s[i] != char:
- return strs[0][:i]
- return strs[0]
- M-2:
- #Sort the array and compare the first and last string
- class Solution:
- def longestCommonPrefix(self, strs: List[str]) -> str:
- if not strs:
- return ""
- strs.sort()
- first = strs[0]
- last = strs[-1]
- i = 0
- while i < len(first) and i < len(last) and first[i] == last[i]:
- i += 1
- return first[:i]
Advertisement
Add Comment
Please, Sign In to add comment