Solving LeetCode Challenges: Advanced Techniques for Mastering Algorithm Questions - Longest Substring Without Repeating Characters

The Question:

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

Example 1:

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

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.

Brute Force Solution

Here is the step:

  1. Create the condition if the length of the input string is less than or equal to 1, and return the length of the string if it is. This is because a string with a length of 0 or 1 impossible to have any repeating characters.

  2. The longestSubstring variable is initialized to 0, which will store the length of the longest substring without repeating characters found so far.

  3. Iterates over the indices of the input starting from left to right, using the left variable to keep track of the left endpoint of the substring.

  4. The existChars object is used to keep track of the characters that have been seen in the current substring, and the currentLength variable is used to keep track of the length of the current substring.

  5. The inner loop iterates over the indices of the input string from the current left index to the right endpoint of the substring.

  6. The traceChar variable is set to the character at the current right index.

  7. If the current character has noon been existed before, the currentLength is incremented, the current character is added to the existChars object with a value of 1, and the longestSubtring variable is updated to the maximum of it is current value and the currentLenght

  8. If the current character has existed before, the inner loop is terminated, and the outer loop moves on to the next left index.

  9. The last we return the variable of longestSubtring which is represent the length of the longest substring without repeating the character

Here is the Code

if(s.length <= 1) return s.length;

    let longestSubstring = 0;

    for(let left = 0; left < s.length; left++) {
        let existChars = {}, currentLength = 0;

        for(let right = left; right < s.length; right++) {
            const traceChar = s[right];

            if(existChars[traceChar] !==1) {
                currentLength++;
                existChars[traceChar] = 1;
                longestSubstring = Math.max(longestSubstring, currentLength);
                 if (longestSubstring <= currentLength){
                     longestSubstring =currentLength
                 }
            } else {
                break;
            }
        }
    }

    return longestSubstring;

Optimal Solution

Here is the Step:

  1. Create the variable for two-pointer, we can name it leftPointer and rightPointer .

  2. Create the variable hashmap to save the current Char, we can name is subMap.

  3. Create the variable to track the longest value that we get, we can name it longest.

  4. Now we can set the looping for managing to movement index of right until how much the length of the array is.

  5. In the looping, create the currentChar variable to save the value of the current substring that we get.

  6. After that the create the checkingChar variable to checking the status of the value current hashMap (subMap).

  7. Now we will check if the the checking char is exist and the value is more than the leftPointer then we reassign leftPointer with the value of the rightPointer +1

  8. After that we insert the value of subMap with the key using currentChar and the value of it is the indext of rightPointer

  9. Now to get the difference Distance between pointerLeft and pointerRight with the formula : rightPointer - (leftPointer-1)

  10. Now is the last step we will compare between the longest with the differenceDistance and we will take the high value to return.

Here the code:

 if (inputString.length <= 1) return inputString.length

    let leftPointer = 0;
    const subMap = {};
    let longest = 0;
    for (let rightPointer = 0; rightPointer < inputString.length; rightPointer++){
        const currentChar = inputString[rightPointer];
        const checkingChar = subMap[currentChar];
        if (checkingChar >= leftPointer){
            leftPointer = checkingChar + 1
        }

        subMap[currentChar] = rightPointer
        const differenceDistance = (rightPointer) - (leftPointer-1);

        longest = Math.max(longest, differenceDistance)
    }

    return longest