I tried to solve the problem (you can read description here: https://leetcode.com/problems/longest-common-prefix/) And the following is code I came up with.
It gives prefix
value of the first string in strs
list and compares prefix with every string from the list, popping all characters that are not equal.
class Solution:def longestCommonPrefix(self, strs: List[str]) -> str:prefix = strs[0][0]for i in range(len(strs)):for j in range(len(prefix)):if strs[i][j] != prefix[j]:prefix.pop(prefix[j])return prefix
But this code fails in the very first testcase where strs = ["flower","flow","flight"]
Expected output is "fl"
, while my code returns just "f"
I am struggling to find what is going wrong in my solution. Maybe you can help?