Most substring problems can be solved efficiently using the sliding window technique. 🚀 Day 97/365 — DSA Challenge Solved: Longest Substring Without Repeating Characters Problem idea: We need to find the length of the longest substring that contains no duplicate characters. Efficient approach: Use a sliding window with two pointers. Steps: 1. Keep track of the last seen index of each character 2. Expand the window by moving the right pointer 3. If a character repeats inside the window, move the left pointer to the position after its last occurrence 4. Update the maximum window length This ensures the window always contains unique characters. ⏱ Time: O(n) 📦 Space: O(1) Day 97/365 complete. 💻 268 days to go. Code: https://lnkd.in/dad5sZfu #DSA #Java #SlidingWindow #LeetCode #LearningInPublic
Longest Substring Without Repeating Characters Solution
More Relevant Posts
-
Not all sliding window problems are about sums — sometimes the constraint is on the product, but the pattern still applies. 🚀 Day 103/365 — DSA Challenge Solved: Subarray Product Less Than K Problem idea: We need to count the number of subarrays where the product of all elements is strictly less than k. Efficient approach: Use a sliding window that maintains a valid product. Steps: 1. Expand the window by multiplying the current element 2. If product becomes ≥ k, shrink the window from the left 3. Keep dividing until product < k 4. Count all valid subarrays ending at each index This works because all numbers are positive, so the window can be adjusted greedily. ⏱ Time: O(n) 📦 Space: O(1) Day 103/365 complete. 💻 262 days to go. Code: https://lnkd.in/dad5sZfu #DSA #Java #SlidingWindow #LeetCode #LearningInPublic
To view or add a comment, sign in
-
-
Some problems break the standard sliding window pattern — especially when negative numbers are involved. 🚀 Day 104/365 — DSA Challenge Solved: Shortest Subarray with Sum at Least K Problem idea: We need to find the length of the shortest subarray whose sum is at least k. The challenge is that the array can contain negative numbers, so normal sliding window won't work. Efficient approach: Use Prefix Sum + Monotonic Deque. Steps: 1. Build a prefix sum array 2. Use a deque to store indices of useful prefix sums 3. While current sum − smallest prefix ≥ k → update answer 4. Maintain increasing order in deque by removing larger prefix sums from the back 5. This ensures we always get the shortest valid subarray This approach efficiently handles negative numbers while keeping optimal time complexity. ⏱ Time: O(n) 📦 Space: O(n) Day 104/365 complete. 💻 261 days to go. Code: https://lnkd.in/dad5sZfu #DSA #Java #SlidingWindow #Deque #PrefixSum #LeetCode #LearningInPublic
To view or add a comment, sign in
-
-
Many subarray problems follow the same hidden pattern — once you recognize it, they become much easier. 🚀 Day 102/365 — DSA Challenge Solved: Count Number of Nice Subarrays Problem idea: We need to count the number of subarrays that contain exactly k odd numbers. Efficient approach: Use the same trick as previous problem: subarrays with exactly k odds = subarrays with ≤ k odds − subarrays with ≤ (k − 1) odds Steps: 1. Use a sliding window to count subarrays with at most k odd numbers 2. Expand window by moving right pointer 3. If odd count exceeds k, shrink window from left 4. Add valid subarrays ending at each index 5. Subtract results to get exact count This pattern works beautifully for binary-like conditions. ⏱ Time: O(n) 📦 Space: O(1) Day 102/365 complete. 💻 263 days to go. Code: https://lnkd.in/dad5sZfu #DSA #Java #SlidingWindow #LeetCode #LearningInPublic
To view or add a comment, sign in
-
-
Day 56/75 — Sliding Window Maximum Today’s problem was about finding the maximum element in every sliding window of size k. Approach: • Use a deque to store indices of useful elements • Maintain decreasing order in deque (front = max) • Remove elements out of current window Key logic: while (!dq.isEmpty() && nums[dq.peekLast()] < nums[i]) { dq.pollLast(); } if (!dq.isEmpty() && dq.peekFirst() == i - k) { dq.pollFirst(); } dq.offerLast(i); Time Complexity: O(n) Space Complexity: O(k) A classic problem that strengthens sliding window + monotonic deque concepts. 56/75 🚀 #Day56 #DSA #SlidingWindow #Deque #Java #Algorithms #LeetCode
To view or add a comment, sign in
-
-
Solved “Longest Repeating Character Replacement” today — this one really helped me understand sliding window better. At first, I was thinking in terms of overall frequency, but that approach fails because the problem is about a contiguous substring, not the whole string. The key idea that clicked was: Instead of fixing the window size, let it grow and shrink based on a condition. For any window: (window size - max frequency) ≤ k This tells us how many characters we need to replace. If it exceeds k, we shrink the window from the left. One interesting part was that we don’t need to perfectly maintain max frequency while shrinking — even a slightly outdated value still works correctly. This problem really reinforced an important pattern: Don’t decide the window size — let the condition control it. #dsa #slidingwindow #java #coding #problemSolving
To view or add a comment, sign in
-
-
🚀 Day 78 of #100DaysOfCode Solved 443. String Compression on LeetCode 🔗 🧠 Key Insight: We compress the array in-place by: 👉 Replacing consecutive characters with: char + count (if count > 1) Example: ["a","a","b","b","c","c","c"] → ["a","2","b","2","c","3"] ⚙️ Approach (Two Pointers): 1️⃣ Use two pointers: 🔹 i → iterate through array 🔹 idx → position to write compressed result 2️⃣ For each character: 🔹 Count consecutive occurrences using a loop 3️⃣ Write character: 🔹 chars[idx++] = ch 4️⃣ If count > 1: 🔹 Convert count to string 🔹 Add each digit to array 5️⃣ Continue until end 6️⃣ Return idx (new length) ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #Strings #TwoPointers #Array #Java #InterviewPrep #CodingJourney
To view or add a comment, sign in
-
-
Many array problems that involve maximizing a window can be solved using the sliding window technique. 🚀 Day 98/365 — DSA Challenge Solved: Max Consecutive Ones III Problem idea: We need to find the maximum number of consecutive 1's in a binary array if we are allowed to flip at most k zeros. Efficient approach: Use a sliding window that allows at most k zeros inside the window. Steps: 1. Expand the window by moving the right pointer 2. Count how many zeros are inside the window 3. If zero count becomes greater than k, move the left pointer until zeros ≤ k 4. Track the maximum window length This keeps the window valid while maximizing the number of 1's. ⏱ Time: O(n) 📦 Space: O(1) Day 98/365 complete. 💻 267 days to go. Code: https://lnkd.in/dad5sZfu #DSA #Java #SlidingWindow #LeetCode #LearningInPublic
To view or add a comment, sign in
-
-
🚀 Day 13 – DSA Practice Today I explored the Sliding Window technique using the problem: Maximum Sum Subarray of Size K 📌 Problem: Given an array and a window size k, find the maximum sum of any subarray of size k. 💡 Approach: Instead of recalculating sum every time, maintain a window sum: • Add next element • Remove previous element This helps process the array in a single pass ⏱ Time Complexity: O(n) 📦 Space Complexity: O(1) This problem helped me understand how sliding window optimizes subarray problems efficiently. #Java #DSA #SlidingWindow #CodingInterview #ProblemSolving
To view or add a comment, sign in
-
-
Day 69/75 — Reverse String Simple and clean two-pointer problem. Approach: • Use two pointers (start & end) • Swap characters • Move pointers inward Key idea: while (i < j) { char temp = s[i]; s[i] = s[j]; s[j] = temp; i++; j--; } Time Complexity: O(n) Space Complexity: O(1) Basic problem, but strengthens in-place thinking and pointer control. 69/75 🔁 #Day69 #DSA #TwoPointers #Strings #Java #LeetCode
To view or add a comment, sign in
-
-
“Most people try to overcomplicate this one… but the simplest approach wins.” Day 69 — LeetCode Progress Problem: Height Checker Required: Given an array of student heights, return the number of indices where the heights are not in the expected non-decreasing order. Idea: If we sort the array, we get the expected order. Now just compare the original array with the sorted version — mismatches are the answer. Approach: Create a copy of the original array Sort the copied array Traverse both arrays: Compare elements at each index If they differ → increment count Return the count Time Complexity: O(n log n) Space Complexity: O(n) #LeetCode #DSA #Java #Arrays #Sorting #ProblemSolving #CodingJourney
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
Great 👍