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
Sliding Window Problem Solving with Dynamic Window Size
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
-
-
🚀 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
-
-
Day 33/50 🚀 — Valid Palindrome (Two Pointer Approach) Today’s problem was a great mix of string manipulation + two pointers. 🔹 Ignored non-alphanumeric characters 🔹 Handled case-insensitivity 🔹 Compared characters from both ends efficiently Key insight: Instead of preprocessing the string, we can optimize in-place using two pointers, skipping unwanted characters on the go. 💡 This improves both readability and performance. Performance: ⚡ Runtime: 2 ms (99%+) 📦 Memory: Efficient #Day33 #LeetCode #TwoPointers #DSA #Java #CodingJourney #50DaysOfCode #ProblemSolving
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
-
-
𝐃𝐚𝐲 𝟔𝟕 – 𝐃𝐒𝐀 𝐉𝐨𝐮𝐫𝐧𝐞𝐲 | 𝐀𝐫𝐫𝐚𝐲𝐬 🚀 Today’s problem focused on finding the smallest subarray with sum ≥ target. 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐒𝐨𝐥𝐯𝐞𝐝 • Minimum Size Subarray Sum 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡 – 𝐒𝐥𝐢𝐝𝐢𝐧𝐠 𝐖𝐢𝐧𝐝𝐨𝐰 • Maintained a window using two pointers (left & right) • Expanded the window by moving right pointer • Once sum ≥ target: • Shrunk the window from the left • Updated the minimum length This ensures we always keep the smallest valid window. 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠𝐬 • Sliding window works well for subarray problems • Expand → to meet condition • Shrink → to optimize result • Two pointers reduce time complexity significantly 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲 • Time: O(n) • Space: O(1) 𝐓𝐚𝐤𝐞𝐚𝐰𝐚𝐲 Sliding window is about balance — expand when needed, shrink when possible. 67 days consistent 🚀 On to Day 68. #DSA #Arrays #SlidingWindow #LeetCode #Java #ProblemSolving #DailyCoding #LearningInPublic #SoftwareDeveloper
To view or add a comment, sign in
-
-
Keeping the Two-Pointer momentum going! Today's challenge: Valid Palindrome. After using two pointers to reverse arrays and remove duplicates, I applied the exact same pattern to strings. The goal? Check if a string reads the same forwards and backwards (like "racecar"). While you could solve this by creating a reversed copy of the string and comparing the two, that requires O(n) extra memory. The Two-Pointer approach handles it entirely in-place with O(1) space! Here is the logic: • Left Pointer: Starts at the very beginning of the string. • Right Pointer: Starts at the very end. • Compare: If the characters match, great! If they don't, it's not a palindrome. • Move Inward: Step both pointers toward the middle until they cross. It is incredibly satisfying to see how one core algorithmic pattern can be adapted to solve so many different types of problems so efficiently. #DSA #Java #LeetCode #isValidPalindrome
To view or add a comment, sign in
-
-
Day 82 - Kth Smallest Element in a BST Used inorder traversal to find the kth smallest element in a binary search tree. Approach: • Inorder traversal of BST gives sorted order • Maintain a counter while traversing • When count == k, capture the value Time Complexity: O(n) Space Complexity: O(h) #Day82 #LeetCode #BST #BinaryTree #DSA #Java #CodingJourney
To view or add a comment, sign in
-
-
Today I solved: Valid Palindrome 💡 Key Takeaway: The challenge was not just checking a palindrome, but handling edge cases like ignoring non-alphanumeric characters and case sensitivity. 👉 Approach: - Use two pointers (left & right) - Skip non-alphanumeric characters - Compare characters in lowercase 📊 Time Complexity: O(n) 🔍 What I learned: Problems that look simple often test attention to detail. Handling edge cases correctly is what makes a solution robust. Clean logic + edge case handling = strong solution 💪 #DSA #LeetCode #Java #CodingJourney #SoftwareEngineering
To view or add a comment, sign in
-
🚀 DSA Preparation 💪 Solved a simple yet important String + Sliding Window problem. Focused on checking unique characters in fixed-size substrings efficiently. Great practice for improving window-based pattern recognition 🔥 🧠 Problem 🔎 Substrings of Size Three with Distinct Characters Given a string s, return the number of good substrings of length 3. 👉 A substring is good if all characters are distinct. 👉 Count all such substrings (including duplicates if they appear multiple times). Example Input: s = "xyzzaz" Output: 1 Input: s = "aababcabc" Output: 4 Improving DSA with small patterns and clean logic 🚀 #DSA #LeetCode #Strings #SlidingWindow #Java #ProblemSolving #Consistency
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
-
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