d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Problem With Longest Substring Without Repeating C
Add Reply New Topic New Poll
Member
Posts: 3
Joined: Jan 31 2023
Gold: 0.00
Apr 11 2023 07:26am
Hello,

I am having trouble understanding the solution to the Longest Substring Without Repeating Characters problem. I have read this problem post, but I am still having trouble figuring out how the code works.

I have attached the code below.

Code
def lengthOfLongestSubstring(self, A):
n = len(A)
if n == 0:
return 0
start,end,ans = 0,0,1
curr = set()
curr.add(A[0])
while end < n-1:
end += 1
if A[end] not in curr:
curr.add(A[end])
ans = max(ans,end-start+1)
else:
while A[start] != A[end]:
curr.remove(A[start])
start += 1
start += 1
return ans


I would really appreciate any help understanding this code and how it works.

Thank you!

This post was edited by Scout16 on Apr 11 2023 07:27am
Retired Moderator
Posts: 7,056
Joined: Dec 19 2013
Gold: 2,948.60
Trader: Trusted
Apr 11 2023 11:07am
def lengthOfLongestSubstring(self, A):

This line defines a function lengthOfLongestSubstring that takes in two arguments self and A. self is typically used in object-oriented programming to refer to the current instance of an object, but it appears to be unused in this function. A is a string for which we want to find the length of the longest substring without repeating characters.


n = len(A)
if n == 0:
return 0


These lines initialize a variable n to the length of the input string A. If the length of A is 0, the function immediately returns 0, because the longest substring without repeating characters in an empty string is an empty string.


start,end,ans = 0,0,1

These lines initialize three variables: start, end, and ans. start and end will be used as pointers to keep track of the current substring being examined, while ans will store the length of the longest substring without repeating characters seen so far. These variables are initialized to 0 and 1, respectively.


curr = set()
curr.add(A[0])


These lines initialize a set called curr to keep track of the characters seen in the current substring being examined. The set is initially populated with the first character of A.


while end < n-1:
end += 1
if A[end] not in curr:
curr.add(A[end])
ans = max(ans,end-start+1)


These lines start a loop that will examine each substring of A. The loop condition is that end is less than n-1, because we want to make sure we include the last character of A in the final substring being examined. On each iteration of the loop, we first increment end to include the next character in A. If the character at end is not already in curr, we add it to curr and update ans to be the maximum of its current value and the length of the current substring (which is end-start+1). This is because we want to keep track of the longest substring seen so far.


else:
while A[start] != A[end]:
curr.remove(A[start])
start += 1
start += 1


If the character at end is already in curr, we need to adjust the substring being examined. We do this by moving the start pointer to the next character after the first occurrence of the character at end. We remove all characters before the new start pointer from curr to ensure that we don't count any repeating characters in the new substring.


return ans

Finally, once we have examined all substrings of A, we return the value of ans, which is the length of the longest substring without repeating characters.
Member
Posts: 1
Joined: Apr 14 2023
Gold: 0.00
Apr 14 2023 06:09am
Quote (Soroush @ Apr 11 2023 07:07pm)
def lengthOfLongestSubstring(self, A):

This line defines a function lengthOfLongestSubstring that takes in two arguments self and A. self is typically used in object-oriented programming to refer to the current instance of an object, but it appears to be unused in this function. A is a string for which we want to find the length of the longest substring without repeating characters.


n = len(A)
if n == 0:
return 0


These lines initialize a variable n to the length of the input string A. If the length of A is 0, the function immediately returns 0, because the longest substring without repeating characters in an empty string is an empty string.


start,end,ans = 0,0,1

These lines initialize three variables: start, end, and ans. start and end will be used as pointers to keep track of the current substring being examined, while ans will store the length of the longest substring without repeating characters seen so far. These variables are initialized to 0 and 1, respectively.


curr = set()
curr.add(A[0])


These lines initialize a set called curr to keep track of the characters seen in the current substring being examined. The set is initially populated with the first character of A.


while end < n-1:
end += 1
if A[end] not in curr:
curr.add(A[end])
ans = max(ans,end-start+1)


These lines start a loop that will examine each substring of A. The loop condition is that end is less than n-1, because we want to make sure we include the last character of A in the final substring being examined. On each iteration of the loop, we first increment end to include the next character in A. If the character at end is not already in curr, we add it to curr and update ans to be the maximum of its current value and the length of the current substring (which is end-start+1). This is because we want to keep track of the longest substring seen so far.


else:
while A[start] != A[end]:
curr.remove(A[start])
start += 1
start += 1


If the character at end is already in curr, we need to adjust the substring being examined. We do this by moving the start pointer to the next character after the first occurrence of the character at end. We remove all characters before the new start pointer from curr to ensure that we don't count any repeating characters in the new substring.


return ans

Finally, once we have examined all substrings of A, we return the value of ans, which is the length of the longest substring without repeating characters.


Copy and pasted from ChatGPT. Good job.
Retired Moderator
Posts: 7,056
Joined: Dec 19 2013
Gold: 2,948.60
Trader: Trusted
Apr 14 2023 06:43am
Quote (Flajello @ 14 Apr 2023 08:09)
Copy and pasted from ChatGPT. Good job.


Yep!
Member
Posts: 15,215
Joined: Jul 9 2021
Gold: 466.99
Apr 14 2023 04:49pm
Quote (Soroush @ Apr 14 2023 05:43am)
Yep!


i wanna run some of my gameserver code through that and see what it spits out :rofl:

it's in crystal, not sure if gpt has support for it. i did see Ary Borenszweig post some chatgpt output on their forum, but he mentioned it was pretty wrong

This post was edited by ChocolateCoveredGummyBears on Apr 14 2023 04:51pm
Retired Moderator
Posts: 7,056
Joined: Dec 19 2013
Gold: 2,948.60
Trader: Trusted
Apr 15 2023 07:07am
Quote (ChocolateCoveredGummyBears @ 14 Apr 2023 18:49)
i wanna run some of my gameserver code through that and see what it spits out :rofl:

it's in crystal, not sure if gpt has support for it. i did see Ary Borenszweig post some chatgpt output on their forum, but he mentioned it was pretty wrong


Gpt is really great for basic stuff that you kinda just want to get a visual on without spending an hour or two finding the stack overflow question thats almost the same as yours.

It's a great tool for explanation too, can ask it to go through stuff line by line etc. But sometimes a more in depth video can answer questions you don't even know you have, which is an advantage as well

It also doesn't currently handle huge blocks of code very well, it times out a lot so the longer the block you paste the harder it is to get a good answer
Member
Posts: 15,215
Joined: Jul 9 2021
Gold: 466.99
Apr 15 2023 02:23pm
Quote (Soroush @ Apr 15 2023 06:07am)
Gpt is really great for basic stuff that you kinda just want to get a visual on without spending an hour or two finding the stack overflow question thats almost the same as yours.

It's a great tool for explanation too, can ask it to go through stuff line by line etc. But sometimes a more in depth video can answer questions you don't even know you have, which is an advantage as well

It also doesn't currently handle huge blocks of code very well, it times out a lot so the longer the block you paste the harder it is to get a good answer


I personally don't like it and never used it. Never found the appeal of ai and think it's all over hyped garbage. Bubble will pop soon. I think being taught by a professor and another object that matches your being is mountains better than a deterministic bot

Seems like the media thinks it's the 2nd coming of christ too

This post was edited by ChocolateCoveredGummyBears on Apr 15 2023 02:27pm
Banned
Posts: 14
Joined: Jan 16 2023
Gold: 0.00
May 9 2023 05:58am
Hello this is Gulshan Negi
Well, the provided code implements a solution to the Longest Substring Without Repeating Characters problem using a sliding window approach. The algorithm maintains two pointers, start and end, that represent the beginning and end of the current substring being considered. It also maintains a set called curr that stores the unique characters in the current substring. The end pointer moves to the right, adding each character encountered to curr until a repeating character is encountered. At this point, the start pointer moves to the right, removing characters from curr until the repeating character is removed from the set. Once the repeating character has been removed, the start pointer moves one position further to the right, and the process is repeated with the end pointer. The length of the longest substring seen so far (ans) is updated whenever a new substring with a greater length is found. The algorithm returns the maximum length seen so far.
Thanks
Member
Posts: 34,230
Joined: May 12 2006
Gold: 0.00
Jun 1 2023 10:36am
Quote (ChocolateCoveredGummyBears @ 15 Apr 2023 16:23)
I personally don't like it and never used it. Never found the appeal of ai and think it's all over hyped garbage. Bubble will pop soon. I think being taught by a professor and another object that matches your being is mountains better than a deterministic bot

Seems like the media thinks it's the 2nd coming of christ too


I am very much on the opposite end of this. The professor has a much more limited scope of knowledge, costs exponentially more, and isn't on-demand 24/7.

I started with zero coding experience and was able to get automations for several tasks at work completed in just a couple of hours by explaining in detail what I needed, asking it to output the code, and then feeding it sections that needed adjusting and what the adjustment was.

If you don't believe LLM's and generative AI are the future... you're going to have a LOT of catching up to do in the coming months/years. The possibilities are damn near limitless already, and getting more refined by the day.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll