1

I am doing 'The Longest Possible Prefix' problem on LeetCode and to solve it I have implemented the code:

strs = ["flower","flow","flight"]
s = ['']
b = []
a = 1
while s.count(s[0])==len(s) and s[0] != strs[0]:
    b = s
    s.clear()
    for i in strs:
        s.append(i[0:a])
    a+=1
print(b[0])

I have used an extra variable b to keep a previous version of the list s before I update it. The problem is that even when the while loop has broken b has again been set to the value of s giving a value that I don not want. Why is this happening and how could I fix it?

0 Answers0