0

I'm a novice in Python and I was trying to solve this problem in Leetcode.

https://leetcode.com/problems/longest-substring-without-repeating-characters/

This is my original code just to test my algorithm.

'''

s="abcabcbb"
word=list(s) #Turn the string into list
s0=set()
s1=set()
Strlength=0 #Length of certain substring.
maxlength=0 #the maximum value of Strlength.

for i in range(len(word)):
    s0=s1
    s1.add(word[i]) #if there is a repeating character, nothing will happen.
    if s0==s1:      #if nothing happened above and s0 is identical to s1.
        Strlength=len(s1) #Save the length of a substring.  
        if (Strlength>maxlength):
            maxlength=Strlength
        s1=set() #Delete the saved substring and prepare for a new one.
    
print(maxlength)

'''

However, this didn't work, that it only returned value of 1. Therefore I made a slight edit to find trouble.

'''

s0=s1
print(s0)
s1.add(word[i])
print(s0)

'''

and the result was:

Result Image

I found out that the set s0 is somehow changing by adding a value to set s1.

Why is this happening?

oo oo
  • 1

0 Answers0