0
Anyone please tell me error in my logic.
Given a string s, find the length of the longest substring without repeating characters. Input: s = "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3. str=input () l=[] for i in str: If i not in l: l.append(i) print (len(l)) #please check the logic not indentation.
19 Answers
+ 2
A͢J correct thank you
+ 1
Akash Gupta
Also you have to print outside the loop.
+ 1
Akash Gupta
Yes you may print inside the loop but you need single and final output not multiple output.
+ 1
Akash Gupta That said, your code finds the number of non repeating chars, not the longest substring length.
My suggestion:
Create an empty string variable to hold substrings
Loop through all string chars
Append each char to substring var
If char exists in substring, delete beginning of substring up to char
Append char to substring
Get substring length
Store maximum length
+ 1
Your (if ) syntax is wrong
Check
str=input ()
l=[]
for i in str:
if i not in l:
l.append(i)
print (len(l))
+ 1
Abate Mon'y
That is what I wanted to suggest him but he said answer is "abc" but set will give different.
0
WOLF read the text carefully i said see the logic not indentation because i write here
0
A͢J it doesn't affect the logic??
You may print inside the loop also
0
A͢J if you solve that let me show that.
0
Akash Gupta Indentation is crucial in Python. To pass it correctly in the question - and allow people to test the actual code, instead of a manual copy - do it this way:
1. Edit your question description
2. Tap "+" button
3. Select Code
4. Choose the relevant code
0
values="abcabccd"
print(len(set(values)))
0
Abate Mon'y Your code has the same problem: finds the number of distinct chars, not the longest substring length
0
VIVEK SHARMA R What's the difference between your code and the OPs? I can't find it.
0
Emerson Prado i don't get it well ...
Is he trying to get longest repeated sub string in a string ?
0
Abate Mon'y He needs the length of the longest substring without repeated chars.
The confusing part is that the string provided gives the same results for the two solutions. But let's undo the confusion.
Take an input as "adacaba". The longest substrings without repeated chars are "dac" or "cab". Any bigger one repeats "a". So the length is 3.
The set solution gives set("abcd"), with length 4.
0
Emerson Prado i get it now🤞
0
Emerson Prado from the above comment i came up with this solution
https://code.sololearn.com/c3R79KlZjqx0/?ref=app
0
Abate Mon'y see this inputs i think you may understand the question properly.
Example 2:
Input: s = "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.
Example 3:
Input: s = "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3.
Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.