🔥 Day 83/100 of Code – Substrings Containing All Three Characters: Counting with Minimal Valid Window! Today solved a substring counting problem by leveraging the "minimal valid window" property: ✅ Problem 1358: Number of Substrings Containing All Three Characters Task: Count substrings containing at least one a, b, and c. Approach: Sliding window with earliest valid start tracking: For each ending index r, find the smallest l such that s[l..r] contains all three chars Once found, all substrings starting from 0..l and ending at r are valid → add l+1 to count Use a frequency map or array to track counts of a,b,c and shrink window when possible Key Insight: If the minimal valid window ending at r starts at minStart, then any substring starting before minStart and ending at r also contains all three characters — giving minStart + 1 valid substrings per r. Complexity: O(n) time, O(1) space — efficient single pass. A smart counting trick that turns a "contain all" constraint into an elegant O(n) accumulation! 🔤🔢 #100DaysOfCode #LeetCode #Java #SlidingWindow #String #SubstringCounting #Algorithm
Substring Counting with Minimal Valid Window in Java
More Relevant Posts
-
🔥 Day 84/100 of Code – Maximum Points from Cards: Sliding Window on a Circular Array! Today solved a problem that cleverly reframes picking from ends as finding the minimum subarray sum in the middle: ✅ Problem 1423: Maximum Points You Can Obtain from Cards Task: Pick k cards from either end of the array to maximize sum. Approach: Complementary sliding window: Total sum of picking k from ends = total sum of array - sum of middle subarray of length n-k Find minimum sum of any subarray of length n-k Answer = total sum - min middle sum Implemented with initial leftSum of first k cards, then slide by removing from left, adding from right Key Insight: Instead of simulating all combinations of left/right picks, think about the unchosen middle segment — minimizing it maximizes chosen ends. Complexity: O(n) time, O(1) space — efficient two-pass solution. A great example of how reframing the problem reveals a simpler sliding window approach! 🃏➕➖ #100DaysOfCode #LeetCode #Java #SlidingWindow #Array #Algorithm #CodingInterview
To view or add a comment, sign in
-
-
LeetCode POTD #3013 - Divide an Array Into Subarrays With Minimum Cost II (02 February 2026) This one looks messy at first glance, but the trick is how you frame it. The first subarray is forced to start at index 0. So the real decision is where the last subarray starts. Fix the starting index of the last subarray at i. Now the constraint i(k-1) - i1 <= dist forces the remaining k-2 subarrays to start inside the window: [i - dist, i - 1] At that point, the problem becomes clean: From a sliding window of length dist, continuously maintain the smallest k-2 values. To do this efficiently: -> Use two ordered sets -> One keeps the k-2 smallest elements -> The other holds the rest -> Maintain the sum while the window slides Total cost for each i: nums[0] + nums[i] + sum_of_k-2_smallest Minimum across all valid i is the answer. Key takeaway: Hard problems usually aren’t about complex code -> they’re about choosing the right perspective. Time Complexity -> O(n log n) Space Complexity -> O(n) #LeetCode #POTD #HardProblems #DataStructures #SlidingWindow #Java #ProblemSolving
To view or add a comment, sign in
-
-
#200DaysOfCode – Day 111 Word Search Problem: Given a 2D grid of characters and a word, determine whether the word exists in the grid. The word must be formed using adjacent cells (up, down, left, right), and each cell can be used only once. Example: Input: board = [[A, B, C, E], [S, F, C, S], [A, D, E, E]] word = "ABCCED" Output: true My Approach: Used DFS (Depth First Search) starting from every cell. Matched characters step-by-step with the given word. Marked cells as visited during the path to avoid reuse. Applied backtracking to explore all possible directions safely. Time Complexity: O(m × n × 4^L) Space Complexity: O(L) (recursion stack) Grid problems often look complex, but breaking them down with DFS + backtracking makes the logic clear and manageable. Patience and careful state handling are the real keys here #takeUforward #100DaysOfCode #Java #LeetCode #ProblemSolving #Backtracking #DFS #DataStructures #Algorithms #CodeNewbie #Consistency
To view or add a comment, sign in
-
-
🔥 Day 86/100 of Code – Subarrays with K Different Integers: The “At Most” Pattern Strikes Again! Today revisited a powerful counting technique to solve a distinct-value subarray problem efficiently: ✅ Problem 992: Subarrays with K Different Integers Task: Count subarrays with exactly k distinct integers. Approach: "At most K distinct" subtraction method: Helper: atMost(k) = subarrays with ≤ k distinct integers Answer = atMost(k) - atMost(k-1) atMost uses sliding window + HashMap to track frequency of values Expand right, shrink left if distinct count > k, count valid subarrays ending at r Key Insight: Converting “exactly K distinct” into “at most K distinct” avoids complex exact matching logic — a reusable pattern seen in Problems 930 and 1248. Complexity: O(n) time, O(k) space — efficient and clean. When you master a pattern, solving variations becomes systematic! 🔄🧠 #100DaysOfCode #LeetCode #Java #SlidingWindow #HashMap #SubarrayCounting #Algorithm
To view or add a comment, sign in
-
-
🚀 Day 46/50 – LeetCode POTD 🔍 3. Longest Substring Without Repeating Characters 📊 Medium 🧠 Key Idea (Sliding Window + Two Pointers) Whenever a problem asks for a substring without repeating characters, the best approach is 👉 Sliding Window. ✔️ Use two pointers: left (l) and right (r) ✔️ Use a boolean array (arr[128]) to track characters (ASCII set) ✔️ If the current character is not present in the window ➡️ expand the window (r++) ➡️ update the maximum length ✔️ If a duplicate character is found ➡️ shrink the window from the left (l++) ➡️ remove characters until the duplicate is gone ✔️ The window always contains unique characters 📌 Example s = "abcabcbb" ➡️ Longest substring = "abc" ➡️ Output = 3 ⚙️ How the Code Works (Java) arr[ch] = true → character exists in the current window On duplicate → move the left pointer and clear characters Track the answer using maxLen = Math.max(maxLen, r - l + 1) ⚙️ Complexity Analysis ⏱ Time Complexity: O(n) 💾 Space Complexity: O(1) (fixed-size array of 128 characters) 💡 Key Takeaway “Substring + uniqueness constraint” ➡️ Think Sliding Window ➡️ Two pointers give an optimal linear-time solution ➡️ Avoid brute force and nested loops 🔁 Consistency beats motivation 🚀 #LeetCode #DSA #SlidingWindow #TwoPointers #Strings #Java #ProblemSolving #CodingJourney #50DaysChallenge
To view or add a comment, sign in
-
-
Day 51/100 – LeetCode Challenge ✅ Problem: #3634 Minimum Removals to Balance Array Difficulty: Medium Language: Java Approach: Sorting + Sliding Window Time Complexity: O(n log n) Space Complexity: O(1) Key Insight: After sorting, valid subarray must satisfy nums[j] ≤ nums[i] × k for all elements. Find longest valid subarray using sliding window, then minimum removals = n - maxLen. Solution Brief: Sorted array to bring comparable values together. Used two pointers: expand j, shrink i when condition fails. Tracked maximum window length where nums[j] ≤ nums[i] × k. Result = total elements - longest valid subarray length. #LeetCode #Day51 #100DaysOfCode #SlidingWindow #Sorting #Java #Algorithm #CodingChallenge #ProblemSolving #MinimumRemovals #MediumProblem #Array #TwoPointers #DSA
To view or add a comment, sign in
-
-
Day 16 – LeetCode 1004 | Sliding Window Done Right (O(n) Solution) Day 16 of solving problems consistently. Solved LeetCode 1004 – Max Consecutive Ones III. Problem: Given a binary array, you can flip at most k zeros. Return the maximum number of consecutive 1s possible. Brute force checks every subarray → O(n²) → wasteful. A better approach is Sliding Window + Two Pointers → O(n). Core logic: • Expand window with right pointer • Count zeros • If zeros exceed k → move left pointer • Track max window length This problem is a good reminder that: Constraints usually hint the optimal technique. If you see “subarray + max/min + linear time” → think Sliding Window first. Consistency builds skill. 16 days. No breaks. Code + explanation in the video 👇 #LeetCode #DSA #Algorithms #SlidingWindow #TwoPointers #Java #ProblemSolving #CodingInterview #SoftwareDeveloper #100DaysOfCode #TechJourney
To view or add a comment, sign in
-
Day 39/100 – LeetCode Challenge ✅ Problem: #1984 Minimum Difference Between Highest and Lowest of K Scores Difficulty: Easy Language: Java Approach: Sorting + Sliding Window Time Complexity: O(n log n) Space Complexity: O(1) Key Insight: After sorting, the minimum range in k elements must come from consecutive elements in sorted order. Sliding window of size k finds the minimum difference between first and last element in window. Solution Brief: Sorted the array to bring close values together. Initialized answer with first k elements. Slided window across array, updating minimum difference. Finding minimal range in sorted array with sliding window #LeetCode #Day39 #100DaysOfCode #Sorting #SlidingWindow #Java #Algorithm #CodingChallenge #ProblemSolving #MinimumDifference #EasyProblem #Array #Optimization #DSA
To view or add a comment, sign in
-
-
🚀 Day 67 of #100DaysOfCode Today’s problem focused on 2D Prefix Sum optimization — Range Sum Query 2D – Immutable (NumMatrix) 📊 At first glance, this problem looks like repeated matrix traversal, but the real win comes from preprocessing smartly. 📌 Problem Summary You’re given a 2D matrix and multiple queries asking for the sum of elements inside a sub-rectangle. Brute force would be too slow for repeated queries. 🧠 My Approach: Prefix Sum (Row-wise) Precompute prefix sums for each row For every query, calculate the sum in O(rows) time using prefix subtraction Avoid recalculating sums for every query This transforms repeated heavy computation into a fast query process ⚡ ⚙️ Time & Space Complexity Preprocessing: O(rows × cols) Each Query: O(rows) Space: O(rows × cols) 🔥 Key Learning Prefix sums are not limited to 1D arrays— they scale beautifully to 2D problems and are extremely powerful for range queries. Another solid step forward in mastering optimization techniques 💪 On to Day 68 🚀 #Day67 #100DaysOfCode #Java #LeetCode #PrefixSum #DSA #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
Day 17 – LeetCode 1004 | Sliding Window Done Right (O(n) Solution) Day 17 of solving problems consistently. Solved LeetCode 1004 – Max Consecutive Ones III. Problem: Given a binary array, you can flip at most k zeros. Return the maximum number of consecutive 1s possible. Brute force checks every subarray → O(n²) → wasteful. A better approach is Sliding Window + Two Pointers → O(n). Core logic: • Expand window with right pointer • Count zeros • If zeros exceed k → move left pointer • Track max window length This problem is a good reminder that: Constraints usually hint the optimal technique. If you see “subarray + max/min + linear time” → think Sliding Window first. Consistency builds skill. 17 days. No breaks. Code + explanation in the video 👇 #LeetCode #DSA #Algorithms #SlidingWindow #TwoPointers #Java #ProblemSolving #CodingInterview #SoftwareDeveloper #100DaysOfCode #TechJourney
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