diff --git a/0003_longest_substring_without_repeat/longest_substring_without_repeat.c b/0003_longest_substring_without_repeat/longest_substring_without_repeat.c index 7af62c6..51c7c27 100644 --- a/0003_longest_substring_without_repeat/longest_substring_without_repeat.c +++ b/0003_longest_substring_without_repeat/longest_substring_without_repeat.c @@ -5,31 +5,19 @@ int lengthOfLongestSubstring(char *s) { - int offset[128]; - int max_len = 0; + int count[256] = {0}; int len = 0; - int index = 0; + int i, j; - memset(offset, 0xff, sizeof(offset)); - while (*s != '\0') { - if (offset[*s] == -1) { - len++; - } else { - if (index - offset[*s] > len) { - /* not included in sliding window, go on increasing */ - len++; - } else { - /* repetition in sliding window, count from scratch */ - len = index - offset[*s]; - } + for (i = 0, j = 0; s[i] != '\0'; i++) { + count[s[i]]++; + while (count[s[i]] > 1) { + len = i - j > len ? i - j : len; + count[s[j++]] -= 1; } - if (len > max_len) { - max_len = len; - } - offset[*s++] = index++; } - return max_len; + return i - j > len ? i - j : len; } int main(int argc, char **argv) diff --git a/0003_longest_substring_without_repeat/longest_substring_without_repeat.cc b/0003_longest_substring_without_repeat/longest_substring_without_repeat.cc index 08a12e2..af643ca 100644 --- a/0003_longest_substring_without_repeat/longest_substring_without_repeat.cc +++ b/0003_longest_substring_without_repeat/longest_substring_without_repeat.cc @@ -5,26 +5,18 @@ using namespace std; class Solution { public: int lengthOfLongestSubstring(string s) { - vector indexes(128, -1); - int max_len = 0; + vector count(256); int len = 0; + int i, j; - for (int i = 0; i < s.length(); i++) { - if (indexes[s[i]] == -1) { - len++; - } else { - if (i - indexes[s[i]] > len) { - /* not included in sliding window, go on increasing */ - len++; - } else { - /* repetition in sliding window, count from scratch */ - len = i - indexes[s[i]]; - } + for (i = 0, j = 0; i < s.length(); i++) { + count[s[i]]++; + while (count[s[i]] > 1) { + len = i - j > len ? i - j : len; + count[s[j++]] -= 1; } - max_len = max(max_len, len); - indexes[s[i]] = i; } - return max_len; + return i - j > len ? i - j : len; } }; diff --git a/0076_minimum_window_substring/window_substring.c b/0076_minimum_window_substring/window_substring.c index a9e87ab..41f5d0b 100644 --- a/0076_minimum_window_substring/window_substring.c +++ b/0076_minimum_window_substring/window_substring.c @@ -2,6 +2,17 @@ #include #include + +/* sliding window pattern + * while (r < size) { + * // check target condition + * while (target_condition) { + * // calculate minimum length + * // iterate left indicator + * } + * // iterate right indicator + * } + */ static char *minWindow(char *s, char *t) { int i, j, count[256] = { 0 }; @@ -42,8 +53,7 @@ static char *minWindow(char *s, char *t) memcpy(result, s + start, min_len); result[min_len] = '\0'; } else { - result = malloc(1); - result[0] = '\0'; + result = ""; } return result;