aboutsummaryrefslogtreecommitdiff
path: root/min_window_substring.c
blob: efcd3a520e76a8bbe36289341714537211ae5527 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// 76. Minimum Window Substring
#include "leetcode.h"

/*
 * given two strings 's' and 't' of length 'n' and 'm' 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.
 */

char *minWindow(char *s, char *t) {
  int m = strlen(s), n = strlen(t), min = INT_MAX, l = -1, r, match = 0,
      a_idx = -1, r_key, l_key;
  int *alpha = calloc(52, sizeof(int));
  int *alpha_copy = calloc(52, sizeof(int));
  for (int i = 0; i < n; i++)
    (t[i] >= 'a') ? alpha[t[i] - 'a']++ : alpha[t[i] - 'A' + 26]++;

  for (r = 0; r < m; r++) {
    r_key = (s[r] >= 'a') ? r_key = s[r] - 'a' : s[r] - 'A' + 26;

    if (alpha[r_key]) {
      if (l == -1)
        l = r;
      alpha_copy[r_key]++;
      if (alpha_copy[r_key] <= alpha[r_key]) {
        match++;
        if (match == n) {
          l_key = (s[l] >= 'a') ? s[l] - 'a' : s[l] - 'A' + 26;
          while (match == n || !alpha[l_key]) {
            if (alpha[l_key]) {
              alpha_copy[l_key]--;
              if (alpha_copy[l_key] < alpha[l_key]) {
                if ((r - l + 1) < min) {
                  min = r - l + 1;
                  a_idx = l;
                }
                match--;
              }
            }
            l++;
            if (l < m)
              l_key = (s[l] >= 'a') ? s[l] - 'a' : s[l] - 'A' + 26;
            else
              break;
          }
        }
      }
    }
  }
  if (a_idx == -1)
    return "";
  char *ans = malloc((min + 1) * sizeof(char));
  ans[min] = '\0';
  strncpy(ans, &s[a_idx], min);
  return ans;
}

int main() {
  char *s1 = "ADOBECODEBANC", *t1 = "ABC";
  char *s2 = "a", *t2 = "a";
  char *s3 = "a", *t3 = "aa";
  printf("%s\n", minWindow(s1, t1)); // expect: BANC
  printf("%s\n", minWindow(s2, t2)); // expect: a
  printf("%s\n", minWindow(s3, t3)); // expect: null
}