Skip to content

Commit

Permalink
Minumum window substring
Browse files Browse the repository at this point in the history
Signed-off-by: begeekmyfriend <[email protected]>
  • Loading branch information
begeekmyfriend committed Aug 8, 2017
1 parent 4f193d6 commit 51da4ce
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
2 changes: 2 additions & 0 deletions 076_minimum_window_substring/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
gcc -O2 -o test window_substring.c
83 changes: 83 additions & 0 deletions 076_minimum_window_substring/window_substring.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>

static char *minWindow(char *s, char *t)
{
/*
* Declare two "hash map" for ASCII chars
* f[]: represents the char found in s
* m[]: stores the chars in t
*/
int i, f[256], m[256], pat_len = 0;
const int NOT_EXISTED = -1;
const int NOT_FOUND = 0;
memset(m, NOT_EXISTED, sizeof(m));
memset(f, NOT_EXISTED, sizeof(f));

/*
* Go through t, and inital the m[] and f[]
* Notes: a same char can be appeared multiple times.
*/
for (i = 0; t[i] != '\0'; i++) {
m[t[i]] == NOT_EXISTED ? m[t[i]] = 1 : m[t[i]]++;
f[t[i]] = NOT_FOUND;
pat_len++;
}

int start =-1;
int winSize = INT_MAX;
int letterFound = 0;
int begin = 0;
for (i = 0; s[i] != '\0'; i++) {
/* if s[i] is existed in t */
if (m[s[i]] != NOT_EXISTED){
f[s[i]]++;

/* if one char has been found enough times, then do not do letterFound++ */
if (f[s[i]] <= m[s[i]]) {
letterFound++;
}
if (letterFound >= pat_len) {
/*
* Find the beginning of the window
* 1) f[s[begin]] == NOT_EXISTED ===> the char at the `begin` is not in t
* 2) f[s[begin]] > m[s[begin]] ===> a same char appeared more than excepted.
*/
while ( f[s[begin]] == NOT_EXISTED || f[s[begin]] > m[s[begin]] ) {
if ( f[s[begin]] > m[s[begin]] ) {
f[s[begin]]--;
}
begin++;
}
/* Calculate the minimized window size */
if(winSize > i - begin + 1){
start = begin;
winSize = i - begin + 1;
}
}
}
}

char *result = NULL;
if (start>=0 && winSize>0) {
result = malloc(winSize + 1);
memcpy(result, s + start, winSize);
result[winSize] = '\0';
} else {
result = malloc(1);
result[0] = '\0';
}
return result;
}

int main(int argc, char **argv)
{
if (argc != 3) {
fprintf(stderr, "Usage: ./test string pattern\n");
exit(-1);
}
printf("Answer: %s\n", minWindow(argv[1], argv[2]));
return 0;
}

0 comments on commit 51da4ce

Please sign in to comment.