The goal is simple but requires a smart approach: given a string, find the length of the longest substring that contains no repeated characters.
For example:
-
"abcabcbb"→3("abc") -
"bbbbb"→1("b") -
"pwwkew"→3("wke")
Instead of checking every possible substring, I solved it using the Sliding Window + Hash Map technique.
💡 How the approach works
I maintain two important variables:
🔹 start — represents the beginning of the current window
🔹 end — moves through the string character by character
🔹 Map — stores the most recent index of each character
Whenever a character is repeated inside the current window, the start pointer is moved forward to the position after its previous occurrence.
The current window length is calculated as:
end - start + 1
and the maximum length is continuously updated.
⚡ Why Sliding Window?
A brute-force solution could generate many substrings and check each one, resulting in much higher time complexity.
With the sliding window approach, each character is processed efficiently, giving an overall O(n) time complexity and O(n) space complexity in the worst case.
The solution was successfully Accepted on LeetCode, passing 1036/1036 test cases. ✅
This problem was a great reminder that understanding data structures like Hash Maps and techniques like Sliding Window can significantly improve algorithmic efficiency.
Consistent problem-solving, one challenge at a time! 💻🔥
#LeetCode #JavaScript #Coding #Programming #DSA #Algorithms #SlidingWindow #HashMap #WebDevelopment #FrontendDeveloper #JavaScriptDeveloper #CodingJourney #ProblemSolving #100DaysOfCode #Tech #SoftwareDevelopment #DeveloperJourney