Advertisement
Dmaxiya

14. Longest Common Prefix

Nov 1st, 2022
1,046
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.51 KB | None | 0 0
  1. class Solution:
  2.     def longestCommonPrefix(self, strs: List[str]) -> str:
  3.         ans = ""
  4.         for j in range(300):
  5.             flag = True
  6.             for i in range(len(strs)):
  7.                 if j >= len(strs[i]):
  8.                     flag = False
  9.                     break
  10.                 if strs[i][j] != strs[0][j]:
  11.                     flag = False
  12.                     break
  13.             if flag:
  14.                 ans = ans + strs[0][j]
  15.             else:
  16.                 break
  17.         return ans
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement