𝗗𝗮𝘆 𝟴/𝟮𝟬 — 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 🎯 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
LeetCode 3: Longest Substring Without Repeating Characters
More Relevant Posts
-
Two Pointers Pattern — Small Bug, Big Lesson :Today I worked on a classic string problem Reverse only the vowels in a string — without changing the positions of the other characters :Example "leetcode" → "leotcede" .Sounds simple — but I hit an important edge-case bug The Problem I Faced :I used the two-pointer approach one pointer from the left- one pointer from the right- move both until they hit vowels- swap- My logic worked… until it didn’t When the string had no vowels (or pointers crossed while searching), the program crashed with an out-of-bounds error Why? Because my inner loops kept moving the pointers without rechecking boundaries 💡 Key Lesson When using the Two Pointers pattern, if pointers move inside inner loops, you must also check boundary conditions there — not only in the main loop #Java #Algorithms #TwoPointers #ProblemSolving #LearningJourney
To view or add a comment, sign in
-
-
🚀 Day 45 / 100 | Longest Substring Without Repeating Characters Intuition: The problem is to find the length of the longest substring without repeating characters. If a repeated character appears, the substring must be adjusted to remove duplicates. Using a sliding window approach, it maintain a window of unique characters while scanning the string. Approach: O(n) -Use a HashSet to store characters currently in the substring window. -Maintain two pointers: left and right . -Iterate through the string using the right pointer. -If the character already exists in the set, remove characters from the left until the duplicate is removed. -Add the current character to the set. -Update the maximum length of the substring using the window size. -Repeat this process until the end of the string. Complexity: Time Complexity: O(n) Space Complexity: O(n) #100DaysOfCode #Java #DSA #LeetCode #SlidingWindow
To view or add a comment, sign in
-
-
Day 20/30 – LeetCode streak Problem: Check if Binary String Has at Most One Segment of Ones A valid string can have ones only in a single continuous block, and the input has no leading zeros, so it always starts with '1'. Core idea Since 's' starts with '1', any second segment of ones must look like: '1...1 0...0 1...1' → which necessarily contains "01" at the point where ones restart. So if "01" appears anywhere, there are at least two segments of ones → return false. If "01" never appears, all ones are in a single prefix (possibly followed by zeros), so return true. This reduces to a single substring check in 'O(n)' time and 'O(1)' space. Day 20 takeaway: Instead of explicitly counting segments, spotting the forbidden pattern "01" turns the whole logic into a one-line string check that’s still fully correct and optimal. #leetcode #dsa #java #strings #consistency
To view or add a comment, sign in
-
-
Today’s problem: Longest Substring with Exactly K Distinct Characters At first it looks like a brute-force substring problem, but that approach quickly drifts toward (O(n^2)). The key realization was that it follows the sliding window pattern. Key idea: Maintain a dynamic window and track character frequencies with a HashMap. Expand the window from the right If distinct characters exceed k, shrink from the left Update the answer only when the window contains exactly k distinct characters What clicked for me: This problem felt very similar to the Fruit Into Baskets problem I solved earlier. Once the pattern was clear, it became obvious that both problems use the same sliding-window template — only the constraint changes (2 types there vs k distinct characters here). Big takeaway: Many “different” substring problems are actually the same pattern with a small constraint change. Recognizing the pattern is half the solution. #GeeksForGeeks #ProblemOfTheDay #SlidingWindow #Java #LearningInPublic
To view or add a comment, sign in
-
-
Day 35 – Plus One Worked on a problem where a large integer is represented as an array of digits, and the task was to increment the number by one. Key Learnings: Traversing arrays from right to left to simulate digit addition Handling carry when a digit becomes 10 Managing edge cases where all digits are 9 by creating a new array #DSA #Java #Arrays #ProblemSolving #CodingPractice #LeetCode
To view or add a comment, sign in
-
-
🚀 Day 22 – LeetCode 3 Longest Substring Without Repeating Characters Solved using Sliding Window + HashMap. Instead of checking all substrings (O(n²) ❌), I maintained a window of unique characters and jumped the left pointer whenever a duplicate appeared. Time Complexity: O(n) Space Complexity: O(1) Key takeaway: Most substring problems are solved using sliding window patterns, not brute force. Day 22/90 consistency streak continues 🔥 #leetcode #dsa #slidingwindow #java #softwareengineering
To view or add a comment, sign in
-
🚀 Day 89 of #100DaysofLeetCode Problem: Minimum Size Subarray Sum Difficulty: Medium Today’s challenge was about finding the smallest length subarray whose sum is greater than or equal to a given target. Since all elements are positive, this problem is a perfect fit for the Sliding Window technique. Instead of checking all possible subarrays (which would be inefficient), I used two pointers: Expand the window by moving the right pointer and adding to the current sum. Once the sum becomes ≥ target, shrink the window from the left to find the minimum possible length. Keep updating the minimum length during this process. Key takeaways: Positive integers allow efficient window shrinking. Two pointers help reduce time complexity from O(n²) to O(n). Maintain a running sum and dynamically adjust window size. Return 0 if no valid subarray exists. Time Complexity: O(n) Space Complexity: O(1) Another solid sliding window problem completed 💪 #100DaysOfCode #100DaysOfLeetCode #LeetCode #DSA #SlidingWindow #TwoPointers #Java #ProblemSolving #CodingJourney #Algorithms
To view or add a comment, sign in
-
-
Day 7/100 – LeetCode Challenge Problem: Palindrome Number Today’s problem focused on number manipulation without converting the integer into a string. Approach: If number is negative → return false Reverse the digits using modulo (% 10) and division (/ 10) Compare reversed number with original number Logic Used: Extract last digit: rem = x % 10 Build reversed number: rev = rev * 10 + rem Remove last digit: x = x / 10 Complexity: Time: O(log₁₀ n) Space: O(1) Key takeaway: Problems involving digits can often be solved mathematically without string conversion. #100DaysOfCode #LeetCode #DSA #Java #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
𝗗𝗮𝘆 𝟭𝟬/𝟮𝟬 — 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 Solved Roman to Integer using HashMap + Single Traversal. ➤ Approach (O(n), O(1) space): • Store Roman symbols and their values in a HashMap • Traverse the string from left to right • If the current value is less than the next value → subtract • Otherwise → add ➤ Key Insight: Roman numerals mostly add up left to right, but when a smaller value appears before a larger one, it indicates subtraction (like IV, IX, XL). By checking the next character during traversal, we can decide whether to add or subtract — all in one pass. #LeetCode #Java #DSA #HashMap #StringManipulation #ProblemSolving #20DaysChallenge #Consistency
To view or add a comment, sign in
-
-
🚀 Day 29 of #100DaysOfCode Solved 442. Find All Duplicates in an Array on LeetCode ✅ 🧠 Key Insight: Since numbers are in the range 1 → n, each number ideally belongs at index num - 1. By repeatedly swapping elements into their correct positions (cyclic sort), duplicates naturally reveal themselves. ⚙️ Approach Used: 🔹Place each element at its correct index (nums[i] → nums[nums[i] - 1]) 🔹Skip when the element is already in the correct position 🔹After rearrangement, any index i where nums[i] ≠ i + 1 is a duplicate ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(1) (excluding output list) #100DaysOfCode #LeetCode #DSA #Arrays #CyclicSort #Java #ProblemSolving #InterviewPrep #LearningInPublic
To view or add a comment, sign in
-
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development