Longest Common Prefix LeetCode solution in Python
1class Solution:2def longestCommonPrefix(self, strs: List[str]) -> str:3cmp = strs[0]4for i in range(1, len(strs)):5l = 06if len(cmp) > len(strs[i]):7l += len(strs[i])8else:9l += len(cmp)10ans = ""11for j in range(l):12if cmp[j] != strs[i][j]:13if j == 0:14return ""15else:16break17else:18ans += strs[i][j]19cmp = ans20return cmp