+ 2

Confused

So the result of this is 4 despite it supposed to be 3. Confused as to why it's giving 4. def lengthOfLongestSubstring(s): Map = [] for char in s: if char in Map: pass else: Map.append(char) return len(Map) Map.clear() print(lengthOfLongestSubstring('pwwkew'))

25th Jun 2020, 11:16 PM
Prog
Prog - avatar
4 Answers
+ 3
from pwkew pwke get appended to Map so len returns 4 instead of pass you should write Map=[] if you are expecting output 3
25th Jun 2020, 11:20 PM
Abhay
Abhay - avatar
+ 1
But I thought len() started at zero shoudn't it have been 3 anyway? Did it double count or something like that?
25th Jun 2020, 11:59 PM
Prog
Prog - avatar
+ 1
Length is always starting with one.
26th Jun 2020, 12:53 AM
Sandra Meyer
Sandra Meyer - avatar
0
(my answer was not really the best...)
26th Jun 2020, 1:18 AM
Sandra Meyer
Sandra Meyer - avatar