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.

18th Jul 2022, 2:43 PM
Akash Gupta
Akash Gupta - avatar
19 ответов
+ 2
A͢J correct thank you
18th Jul 2022, 3:38 PM
Akash Gupta
Akash Gupta - avatar
+ 1
Akash Gupta Also you have to print outside the loop.
18th Jul 2022, 3:21 PM
A͢J
A͢J - avatar
+ 1
Akash Gupta Yes you may print inside the loop but you need single and final output not multiple output.
18th Jul 2022, 3:33 PM
A͢J
A͢J - avatar
+ 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
19th Jul 2022, 4:57 AM
Emerson Prado
Emerson Prado - avatar
+ 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))
20th Jul 2022, 11:08 AM
Vivek Sharma R
Vivek Sharma R - avatar
+ 1
Abate Mon'y That is what I wanted to suggest him but he said answer is "abc" but set will give different.
20th Jul 2022, 6:07 PM
A͢J
A͢J - avatar
0
WOLF read the text carefully i said see the logic not indentation because i write here
18th Jul 2022, 3:17 PM
Akash Gupta
Akash Gupta - avatar
0
A͢J it doesn't affect the logic?? You may print inside the loop also
18th Jul 2022, 3:28 PM
Akash Gupta
Akash Gupta - avatar
0
A͢J if you solve that let me show that.
18th Jul 2022, 3:29 PM
Akash Gupta
Akash Gupta - avatar
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
19th Jul 2022, 4:56 AM
Emerson Prado
Emerson Prado - avatar
0
values="abcabccd" print(len(set(values)))
20th Jul 2022, 9:39 AM
Abate Mon'y
Abate Mon'y - avatar
0
Abate Mon'y Your code has the same problem: finds the number of distinct chars, not the longest substring length
20th Jul 2022, 6:00 PM
Emerson Prado
Emerson Prado - avatar
0
VIVEK SHARMA R What's the difference between your code and the OPs? I can't find it.
20th Jul 2022, 6:02 PM
Emerson Prado
Emerson Prado - avatar
0
Emerson Prado i don't get it well ... Is he trying to get longest repeated sub string in a string ?
20th Jul 2022, 6:08 PM
Abate Mon'y
Abate Mon'y - avatar
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.
20th Jul 2022, 6:16 PM
Emerson Prado
Emerson Prado - avatar
0
Emerson Prado i get it now🤞
20th Jul 2022, 6:18 PM
Abate Mon'y
Abate Mon'y - avatar
0
Emerson Prado from the above comment i came up with this solution https://code.sololearn.com/c3R79KlZjqx0/?ref=app
20th Jul 2022, 8:32 PM
Abate Mon'y
Abate Mon'y - avatar
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.
26th Jul 2022, 6:55 AM
Akash Gupta
Akash Gupta - avatar