๐Ÿ—“ Daily LeetCode Progress โ€“ Day 19

Problems Solved:

#1004 Max Consecutive Ones III
#438 Find All Anagrams in a String

This continues my daily series (Day 19: Sliding Window โ€” Max Ones + Anagram Indices). Today I worked on two core sliding window problems: maximizing consec…


This content originally appeared on DEV Community and was authored by Ertugrul

Problems Solved:

  • #1004 Max Consecutive Ones III
  • #438 Find All Anagrams in a String

This continues my daily series (Day 19: Sliding Window โ€” Max Ones + Anagram Indices). Today I worked on two core sliding window problems: maximizing consecutive 1s with up to k flips, and finding all starting indices of anagram substrings.

๐Ÿ’ก What I Learned

  • For Max Consecutive Ones III, the sliding window expands with the right pointer and contracts when more than k zeros are in the window. This ensures the longest valid window is always tracked.
  • For Find All Anagrams in a String, maintaining a frequency counter for both the target string p and the current window of s allows us to detect valid anagrams in O(n).
  • Both problems reinforce the sliding window template: grow the window, shrink when invalid, and update results.

๐Ÿงฉ #1004 โ€” Max Consecutive Ones III

Python

class Solution:
    def longestOnes(self, nums: List[int], k: int) -> int:
        n, right = len(nums), 0
        left = 0
        temp_k = k
        max_temp = 0

        while right < n:
            if nums[right] == 0:
                temp_k -= 1

            while temp_k < 0:
                if nums[left] == 0:
                    temp_k += 1
                left += 1

            max_temp = max(max_temp, right - left + 1)
            right += 1

        return max_temp

C++

class Solution {
public:
    int longestOnes(vector<int>& nums, int k) {
        int n = nums.size();
        int left = 0, right = 0;
        int temp_k = k;
        int max_temp = 0;

        while (right < n) {
            if (nums[right] == 0) {
                temp_k--;
            }

            while (temp_k < 0) {
                if (nums[left] == 0) {
                    temp_k++;
                }
                left++;
            }

            max_temp = max(max_temp, right - left + 1);
            right++;
        }

        return max_temp;
    }
};

Time: O(n)
Space: O(1)

๐Ÿงฉ #438 โ€” Find All Anagrams in a String

Python

class Solution:
    def findAnagrams(self, s: str, p: str) -> List[int]:
        n, right = len(s), len(p)

        if right > n:
            return []

        need = Counter(p)
        window = Counter(s[:right])
        start_index = []

        if need == window:
            start_index.append(0)

        for i in range(right, n):
            window[s[i]] += 1
            left_c = s[i - right]
            window[left_c] -= 1
            if window[left_c] == 0:
                del window[left_c]

            if window == need:
                start_index.append(i - right + 1)

        return start_index

C++

class Solution {
public:
    vector<int> findAnagrams(string s, string p) {
        int n = s.size();
        int m = p.size();
        if (m > n) return {};

        vector<int> need(26, 0), window(26, 0);
        for (char c : p) need[c - 'a']++;

        vector<int> res;
        int left = 0, right = 0;

        while (right < n) {
            window[s[right] - 'a']++;
            right++;

            if (right - left == m) {
                if (window == need) res.push_back(left);
                window[s[left] - 'a']--;
                left++;
            }
        }
        return res;
    }
};

Time: O(n)
Space: O(26) โ‰ˆ O(1)

๐Ÿ“ธ Achievements

  • Implemented sliding window expansion/shrink for max ones with flips.

maxp

maxcpp

  • Found all anagram indices using frequency matching in Python and C++.

anagpy

anagcpp

๐Ÿ“ฆ Complexity Recap

  • Max Consecutive Ones III: O(n) time, O(1) space.
  • Find All Anagrams in a String: O(n) time, O(1) space.

๐Ÿ“ฃ Join the Journey

Iโ€™m solving and documenting problems daily in both Python and C++, covering arrays, sliding window, linked lists, and trees. Follow along if youโ€™re interested in systematic problem solving.

Links


This content originally appeared on DEV Community and was authored by Ertugrul


Print Share Comment Cite Upload Translate Updates
APA

Ertugrul | Sciencx (2025-09-06T11:59:31+00:00) ๐Ÿ—“ Daily LeetCode Progress โ€“ Day 19. Retrieved from https://www.scien.cx/2025/09/06/%f0%9f%97%93-daily-leetcode-progress-day-19/

MLA
" » ๐Ÿ—“ Daily LeetCode Progress โ€“ Day 19." Ertugrul | Sciencx - Saturday September 6, 2025, https://www.scien.cx/2025/09/06/%f0%9f%97%93-daily-leetcode-progress-day-19/
HARVARD
Ertugrul | Sciencx Saturday September 6, 2025 » ๐Ÿ—“ Daily LeetCode Progress โ€“ Day 19., viewed ,<https://www.scien.cx/2025/09/06/%f0%9f%97%93-daily-leetcode-progress-day-19/>
VANCOUVER
Ertugrul | Sciencx - » ๐Ÿ—“ Daily LeetCode Progress โ€“ Day 19. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/09/06/%f0%9f%97%93-daily-leetcode-progress-day-19/
CHICAGO
" » ๐Ÿ—“ Daily LeetCode Progress โ€“ Day 19." Ertugrul | Sciencx - Accessed . https://www.scien.cx/2025/09/06/%f0%9f%97%93-daily-leetcode-progress-day-19/
IEEE
" » ๐Ÿ—“ Daily LeetCode Progress โ€“ Day 19." Ertugrul | Sciencx [Online]. Available: https://www.scien.cx/2025/09/06/%f0%9f%97%93-daily-leetcode-progress-day-19/. [Accessed: ]
rf:citation
» ๐Ÿ—“ Daily LeetCode Progress โ€“ Day 19 | Ertugrul | Sciencx | https://www.scien.cx/2025/09/06/%f0%9f%97%93-daily-leetcode-progress-day-19/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.