📘 DSA Journey — Day 25 Today’s focus: Sliding Window with constraint tracking. Problem solved: • Find the Longest Semi-Repetitive Substring (LeetCode 2730) Concepts used: • Sliding Window / Two-pointer technique • Constraint tracking within window • Dynamic window adjustment Key takeaway: The goal is to find the longest substring where there is at most one pair of equal adjacent characters. Using a sliding window, we expand the window while keeping track of how many adjacent equal pairs exist. If the number of such pairs exceeds 1, we shrink the window from the left until the condition is satisfied again. At each step, we maintain the maximum valid window length. This problem shows how sliding window can be adapted not just for counts or sums, but also for tracking specific patterns or conditions inside a substring. Continuing to strengthen pattern recognition and consistency in DSA problem solving. #DSA #Java #LeetCode #CodingJourney
Sliding Window Problem Solving with Constraint Tracking
More Relevant Posts
-
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
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
-
-
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
-
-
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
-
-
🚀 DSA Journey: 15/75 Completed 📌 Problem: Maximum Number of Vowels in a Substring of Given Length Today’s problem was a great example of the Sliding Window technique. 🔍 Key Idea: Instead of checking every substring (which is inefficient), we maintain a window of size k and: Add the next character to the window Remove the previous character Track the maximum number of vowels 💡 This reduces time complexity to O(n) — much better than brute force! 🧠 What I Learned: How to efficiently manage a fixed-size window Importance of updating values while sliding Avoiding unnecessary recalculations 🔥 Progress: 15 / 75 problems done — consistency is the real key! #DSA #CodingJourney #Java #LeetCode #100DaysOfCode #ProblemSolving
To view or add a comment, sign in
-
-
🚀 DSA Preparation 💪 Solved an interesting Array + Sliding Window problem on maximizing score. Instead of picking cards directly, learned to think in reverse by finding the minimum subarray to exclude 🔥 This approach helps in optimizing the solution efficiently 🚀 🧠 Problem 🔎 Maximum Points You Can Obtain from Cards Given an array cardPoints and an integer k, you can take cards from the beginning or end. 👉 You must take exactly k cards. 👉 Return the maximum score possible. Example Input: cardPoints = [1,2,3,4,5,6,1], k = 3 Output: 12 Input: cardPoints = [2,2,2], k = 2 Output: 4 Input: cardPoints = [9,7,7,9,7,7,9], k = 7 Output: 55 Improving DSA with better thinking and optimization 🚀 #DSA #LeetCode #SlidingWindow #Arrays #Java #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
🔥 Day 57 Completed of #GFG160 (160 Days DSA Challenge) 📌 Problem Solved: Longest substring with distinct characters 🧠 Topic: Sliding Window | Hashing | Strings ⤴️ Level: Medium 💻 Language: Java 📍 Platform: GeeksforGeeks 📊 Problem Summary: Given a string, find the length of the longest substring without repeating characters. The substring must contain all distinct characters. 🛠️ Approach Used: Use a sliding window with two pointers (start and end). Maintain an array to store the last index where each character appeared. As we expand the window by moving end, if the current character was seen before and its last index is within the current window, move start just after that index. Update the character's last index and track the maximum window length. ⚠️ Insight: The key is jumping start directly to the position after the last occurrence of the repeating character - not just incrementing by one. This ensures we always maintain a valid window with all distinct characters in O(n) time. 📚 Takeaways: • Learned the optimized sliding window with last-index tracking • Understood why start jumps to last occurrence + 1 instead of incremental moves • Achieved 100% accuracy with 1111 test cases passed in just 0.05 seconds • Scored 4 points 🚀 Progress Update: Sliding window mastery continues - from arrays to strings, from counting to longest substring problems. #Day57 #DSA #SlidingWindow #LongestSubstring #Strings #Java #GeeksforGeeks #GFG160 #Algorithms #ProblemSolving #InterviewPrep #Consistency #GeeksforGeeks #SkillUpWithGFG #NationSkillUp
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
-
-
🚀 𝗗𝗮𝘆 - 36/60 Problem: Indexes of Subarray Sum 🔍 Learned: Since the array contains only non-negative numbers, used the sliding window (two-pointer) approach. Expand the window by increasing the right pointer and shrink it when the sum exceeds the target. 😅 Struggles: Initially thought about prefix sum + hashmap, but that’s unnecessary here. Realizing that non-negative elements allow sliding window made the solution much simpler. 🧠 Key Learning: Sliding window works perfectly when elements are non-negative because the sum behaves predictably. This helps achieve O(n) time complexity efficiently. 📦 Concepts Used: #Arrays #SlidingWindow #TwoPointers #Subarrays #TimeComplexity #DSA Right approach depends on constraints—understanding that is the real skill. 🚀 #Java #CodingJourney #ProblemSolving #DSA
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