Skip to content

3. Longest Substring Without Repeating Characters #34

Answered by mah-shamim
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

To solve this problem, we can follow these steps:

  1. Initialize Variables:

    • maxLength to store the maximum length of substring found.
    • charIndexMap to store the last index of each character encountered.
    • start to represent the starting index of the current window.
  2. Sliding Window:

    • Iterate through the string with an index end representing the end of the current window.
    • If the character at s[end] is already in the charIndexMap and its index is greater than or equal to start, move the start to charIndexMap[s[end]] + 1.
    • Update the charIndexMap with the current index of s[end].
    • Calculate the current window length (end - start + 1) and update maxLength if it's larger than the previously recorde…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by basharul-siddike
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
1 participant