LeetCode 3: Longest Substring Without Repeating Characters

𝗗𝗮𝘆 𝟴/𝟮𝟬 — 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 🎯 LeetCode 3 – Longest Substring Without Repeating Characters Given a string, find the length of the longest substring without duplicate characters. ➤ Approach: We use the sliding window technique to dynamically track the current substring and adjust it whenever a duplicate is found. A HashMap is used to store the last seen index of each character for quick lookups. - Use two pointers (left & right) to define the current window. - Move right forward, tracking characters and their last seen index. - If a duplicate is found within the window, move left to just after the previous occurrence. - Continuously update the maximum length found. This ensures each character is processed at most twice, making the approach highly efficient. ➤ Key Insight: By using the HashMap to remember the last index of characters, we avoid re-checking parts of the string — a prime example of turning a brute-force O(n²) problem into an O(n) solution. ➤ Time Complexity: O(n) ➤ Space Complexity: O(min(n, charset size)) #LeetCode #Java #DSA #TwoPointers #ArrayManipulation #ProblemSolving #20DaysChallenge #Consistency

  • text

To view or add a comment, sign in

Explore content categories