Understanding Longest Substring Without Repeating Characters on LeetCode

Today I finally understood the logic behind the Longest Substring Without Repeating Characters problem from LeetCode — and honestly, the journey of understanding it was more valuable than just getting the answer. At first, the idea of Sliding Window felt confusing. I couldn’t visualize how the substring keeps changing and how the algorithm still finds the correct result. So instead of jumping straight to the code, I broke it down step by step like a beginner: Example string: pwwkew I started tracking a window of characters: [p] [p,w] duplicate w → remove from front [w] [w,k] [w,k,e] duplicate w → remove until duplicate gone [k,e,w] The key insight that finally clicked for me: The window keeps changing, but a separate variable keeps track of the best result. currentLength = end - start + 1 maxLength = Math.max(maxLength, currentLength) So even though the window moves, the algorithm never loses the best answer it has seen so far. This problem helped me understand two important concepts: • Sliding Window technique • Maintaining a running best value (maxLength) Sometimes the real learning happens when you slow down and understand the logic instead of just memorizing the solution. Still learning, still exploring. 🚀 #DSA #Java #ProblemSolving #CodingJourney #SlidingWindow

To view or add a comment, sign in

Explore content categories