Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!

Punit Jain

Ranch Hand
+ Follow
since Aug 20, 2011
Boston. MA
Cows and Likes
Cows
Total received
5
In last 30 days
1
Total given
0
Likes
Total received
17
Received in last 30 days
0
Total given
29
Given in last 30 days
1
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Punit Jain

Liutauras Vilda wrote:Cowgratulations, your post has been published in CodeRanch's August 2020 journal.



Thank you.
3 weeks ago

Campbell Ritchie wrote:Why do you need these counters when you can use Set#size() instead? Please explain how your loop works; I don't understand the logic. I cannot however break your code. Why are you accepting null or empty Strings?



Let's say the input is: "abcabcbb"

It will go into the if condition till the 2nd index. The character at the third index is already present in the set hence it will go into the else block. It will start removing the characters from the set until it encounters that repetitive character (the current value of index 'i'). It will then increment the removevaluecounter and break the while loop. At this point, the set will have only that repetitive character (the current value of index I). It will resume the for loop.

I think the empty string should be size 0, but I can skip the null part.



1 month ago

Piet Souris wrote:@Punit
well done, and have a cow!



thanks so much.    

Piet Souris wrote:
An extra challenge for you: can you extend your method such that it also gives that longest substring, and its starting index?



Sure, how about this:

1 month ago
So here is how I am doing it now:

Approach

1. Iterate through the characters in the string.
2. Keep adding the characters to a data structure that accepts unique values.
3. Store the current length of the data structure in memory.
4. Start removing the values from the string as soon as it encounters a duplicate character in the string.
5. Perform step#1 to 4 until it reaches the end of the string.


Implementation

Iterate through the characters in the string and add them to a HashSet until a repetitive character is found. Keep storing the current length in an integer variable. As it encounters a unique character, start removing from the HashSet while maintaining a remove counter variable.

Code


1 month ago

Campbell Ritchie wrote:But what about the values? What are you doing with the values? Hint: nothing. So why are you using a map? Why don't you use other data structures/types?


Yes, I should use a different data structure that takes unique values.

Campbell Ritchie wrote:You mean O(1) time complexity? There are other data structures giving O(1) time complexity too. And some maps run in O(logn) time complexity. That is for the put() method, at least.


Actually, I meant that I could do it in just one loop.

1 month ago

Campbell Ritchie wrote:But what about the values? What are you doing with the values? Hint: nothing. So why are you using a map? Why don't you use other data structures/types?


Yes, I should use a different data structure that takes unique values.

Campbell Ritchie wrote:You mean O(1) time complexity? There are other data structures giving O(1) time complexity too. And some maps run in O(logn) time complexity. That is for the put() method, at least.


Actually, I meant that I could do it in just one loop.

1 month ago

Liutauras Vilda wrote:
Is that what your string shortening achieves?


expected:
pwwkew
wwkew
wkew
kew
...



Got it. Here is the updated code:
1 month ago

Liutauras Vilda wrote:

Punit Jain wrote:Note that the answer must be a substring, "pwke" is a subsequence and not a substring.


Apart from somewhere incorrect logic what Campbell already mentioned to you, your solution doesn't seem to adhere to the requirements you posted. I've bolded that part.



As per the first line "Given a string, find the length of the longest substring without repeating characters." Also, the return type in the method signature on leetcode is int. So I think that they are expecting the length as I can't change the method signatures.

By this -  "pwke" is a subsequence and not a substring - I think they meant that it should be the length of substring and not a subsequence. But yeah, they should have added the length word in the last line to avoid confusion.
1 month ago

Campbell Ritchie wrote:

Punit Jain wrote:Keep appending the character to the map as map keys.

Why did you say “map”? Anyway, that is an implementation detail, which shouldn't be included in the logic.



I thought to use a map because the keys in map should be unique. When the map encounters a repeating key, it replaces the value of the same existing key. Based on the problem, this way I will know that I need to break/pause once I encounter the same key and store the length to a variable. Another reason I chose a map so that I get o(n).

Campbell Ritchie wrote:

Punit Jain wrote:As soon as I reach to a character that's already added to the map . . .

You mean when you encounter a character already recorded.



Yes, thanks for the correction.

Campbell Ritchie wrote:

Punit Jain wrote:a temp variable.

Another implementation detail.


You mean, I am mixing up logic with the implementation details? I should first build the logic without selecting the specific data structures and then the implementation details (i.e. selecting the data structures)?

Campbell Ritchie wrote:

Punit Jain wrote:. . . remove the already visited characters . . .

Please explain what you mean about removal? Why don't you simply start from the next character? What does line 21 do?


By removal - I meant shortening the string by taking out the already visited string from the original string and start again from index 0. Yes, I can also start with the next character and that would be better as I save the call to substring(). I assume you meant line# 31 then it removes (substring) the already visited string from the actual string and then the next line is to reset the index to 0. Like the following:




Campbell Ritchie wrote:I think you are shortening the text from its beginning wrongly.



Did you mean this? s = s.substring(currentSize, s.length() - 1);
1 month ago

Campbell Ritchie wrote:Have a possible solution, but I am keeping it hidden.

My JShell wrote:SubstringFinder.main( "abcabcbb",  "bbbbb", "pwwkew");
Longest substring = “abc”; length =    3
Longest substring = “b”; length =      1
Longest substring = “wke”; length =    3



Yup, want to improve my logic building.
1 month ago

Campbell Ritchie wrote:Please start by explaining the logic of the method.


The way I approached this is - iterate through each character of the string. Keep appending the character to the map as map keys. As soon as I reach to a character that's already added to the map, stop and get the length of the map and keep it into a temp variable.

Them remove the already visited characters fro the string and start the process again. If the length (before a repeating character arrives again) is greater than the earlier temp variable length, then replace the temp variable with this length.

Campbell Ritchie wrote:Why did you say something about arrays in your documentation comment when the parameter is typed String?



That's my mistake. Should be a String.
1 month ago

Stephan van Hulst wrote:

  • Don't return magic values like -1 if your method can't find an occurrence. Either you expect that a match can be found and you throw an exception when it doesn't, or you return OptionalInt.


  • As per the problem: 'Given a string, find the first non-repeating character in it and return its index. If it doesn't exist, return -1.' Here is the updated code;




    1 month ago
    Solving another leetcode problem:


    Given a string, find the length of the longest substring without repeating characters.

    Example 1:

    Input: "abcabcbb"
    Output: 3
    Explanation: The answer is "abc", with the length of 3.
    Example 2:

    Input: "bbbbb"
    Output: 1
    Explanation: The answer is "b", with the length of 1.
    Example 3:

    Input: "pwwkew"
    Output: 3
    Explanation: The answer is "wke", with the length of 3.
                Note that the answer must be a substring, "pwke" is a subsequence and not a substring.



    Following is my code:



    The logic fails when the string is "pwwkew". Looks like the issue is at line# 32. I reset the value of i to 0, but it gets increased by 1 before the next iteration because of 'i++'. Any suggestions?
    1 month ago
    Technically the map doesn't contain the addends. It contains the numbers and indexes. Maybe


    1 month ago