LeetCode //C - 76. Minimum Window Substring

76. Minimum Window Substring

Given two strings s and t of lengths m and n respectively, return the minimum window substring of s such that every character in t (including duplicates) is included in the window. If there is no such substring, return the empty string “”.

The testcases will be generated such that the answer is unique.

 

Example 1:

Input: s = “ADOBECODEBANC”, t = “ABC”
Output: “BANC”
Explanation: The minimum window substring “BANC” includes ‘A’, ‘B’, and ‘C’ from string t.

Example 2:

Input: s = “a”, t = “a”
Output: “a”
Explanation: The entire string s is the minimum window.

Example 3:

Input: s = “a”, t = “aa”
Output: “”
Explanation: Both 'a’s from t must be included in the window.
Since the largest window of s only has one ‘a’, return empty string.

Constraints:

  • m == s.length

  • n == t.length

  • 1 < = m , n < = 105 1 <= m, n <= 105 1<=m,n<=105

  • s and words[i] consist of lowercase English letters.

From: LeetCode
Link: 30. Substring with Concatenation of All Words


Solution:

Ideas:
This solution uses a variant of the sliding window approach with two pointers, left and j. The idea is to move j forward to scan the string s, while adjusting left to ensure that the substring from left to j contains all the words in words[] with correct frequencies.
Here are the main steps:
1. First, create two arrays sCount and tCount of size 128 (to cover all ASCII characters) to store the frequency of each character in s and t.
2. Fill tCount with the frequency of each character in t.
3. Iterate over s with two pointers, start and end, initially at position 0.
4. Move the end pointer and increment the count of s[end] in sCount.
5. If sCount[s[end]] is less than or equal to tCount[s[end]], increment a counter formed.
6. If formed equals the total number of characters in t, try to contract the window from the start pointer.
7. Update minWindow if the current window is smaller.
Code:
char* minWindow(char* s, char* t) {
    int sCount[128] = {0}, tCount[128] = {0};
    int sLen = strlen(s), tLen = strlen(t);
    int minLen = sLen + 1, minStart = 0;
    int start = 0, end = 0;
    int formed = 0;

    for (int i = 0; i < tLen; i++) {
        tCount[t[i]]++;
    }

    int required = 0;
    for (int i = 0; i < 128; i++) {
        if (tCount[i] > 0) {
            required++;
        }
    }

    while (end < sLen) {
        sCount[s[end]]++;
        if (tCount[s[end]] > 0 && sCount[s[end]] == tCount[s[end]]) {
            formed++;
        }

        while (start <= end && formed == required) {
            if (end - start + 1 < minLen) {
                minLen = end - start + 1;
                minStart = start;
            }

            sCount[s[start]]--;
            if (tCount[s[start]] > 0 && sCount[s[start]] < tCount[s[start]]) {
                formed--;
            }
            start++;
        }
        end++;
    }

    if (minLen == sLen + 1) {
        return "";
    }

    char* res = (char*)malloc((minLen + 1) * sizeof(char));
    strncpy(res, s + minStart, minLen);
    res[minLen] = '\0';

    return res;
}

你可能感兴趣的:(LeetCode,leetcode,c语言,算法)