File tree Expand file tree Collapse file tree 4 files changed +32
-24
lines changed
0076_minimum_window_substring
0438_find_all_anagrams_in_a_string
0516_longest_palindromic_subsequence
0567_permutation_in_string Expand file tree Collapse file tree 4 files changed +32
-24
lines changed Original file line number Diff line number Diff line change @@ -18,31 +18,31 @@ static char *minWindow(char *s, char *t)
1818 int i , j , count [256 ] = { 0 };
1919 int slen = strlen (s );
2020 int tlen = strlen (t );
21+ /* edges of sliding window */
22+ int l = 0 , r = 0 ;
23+ int min_len = slen + 1 ;
24+ int start = 0 ;
25+ int chars_to_meet = 0 ;
26+
2127 for (i = 0 ; i < tlen ; i ++ ) {
2228 count [t [i ]]++ ;
2329 }
2430
25- /* edges of sliding window */
26- int lo = 0 , hi = 0 ;
27- int min_len = slen + 1 ;
28- int start = 0 ;
29- int chars_to_meet = tlen ;
30- while (hi < slen ) {
31- if (-- count [s [hi ++ ]] >= 0 ) {
31+ while (r < slen ) {
32+ if (-- count [s [r ++ ]] >= 0 ) {
3233 /* pattern found */
33- chars_to_meet -- ;
34+ chars_to_meet ++ ;
3435 }
3536
36- while (chars_to_meet == 0 ) {
37- if (hi - lo < min_len ) {
38- min_len = hi - lo ;
39- start = lo ;
37+ while (chars_to_meet == tlen ) {
38+ if (r - l < min_len ) {
39+ min_len = r - l ;
40+ start = l ;
4041 }
4142
4243 /* Chars with negative count are not included in the pattern string */
43- if (++ count [s [lo ++ ]] > 0 ) {
44- /* chars_to_meet == 1 */
45- chars_to_meet ++ ;
44+ if (++ count [s [l ++ ]] > 0 ) {
45+ chars_to_meet -- ;
4646 }
4747 }
4848 }
Original file line number Diff line number Diff line change 55/**
66 * Note: The returned array must be malloced, assume caller calls free().
77 */
8- int * findAnagrams (char * s , char * p , int * returnSize ){
8+ int * findAnagrams (char * s , char * p , int * returnSize )
9+ {
910 * returnSize = 0 ;
1011 int * res = malloc (11000 * sizeof (int ));
1112 int i , pat_len = 0 ;
1213 int count [128 ] = { 0 };
14+ int l = 0 , r = 0 , len = 0 ;
15+
1316 for (i = 0 ; p [i ] != '\0' ; i ++ ) {
1417 count [p [i ]]++ ;
1518 }
1619 pat_len = i ;
1720
18- int l = 0 , r = 0 , len = 0 ;
1921 while (s [r ] != '\0' ) {
2022 if (-- count [s [r ++ ]] >= 0 ) {
2123 len ++ ;
2224 }
23- if (r - l >= pat_len ) {
24- if (len == pat_len ) {
25+
26+ while (len >= pat_len ) {
27+ if (r - l == pat_len ) {
2528 res [(* returnSize )++ ] = l ;
2629 }
2730 if (++ count [s [l ++ ]] > 0 ) {
Original file line number Diff line number Diff line change @@ -13,6 +13,9 @@ int longestPalindromeSubseq(char * s)
1313 int i , j , k ;
1414 int len = strlen (s );
1515 int * * dp = malloc (len * sizeof (int * ));
16+
17+ /* The dp array indicates the length of palindrome subsequence of
18+ * nums[i...j] */
1619 for (i = 0 ; i < len ; i ++ ) {
1720 dp [i ] = malloc (len * sizeof (int ));
1821 memset (dp [i ], 0 , len * sizeof (int ));
Original file line number Diff line number Diff line change 66
77bool checkInclusion (char * s1 , char * s2 )
88{
9- int i , count [128 ] = { -1 }, pat_len = 0 ;
9+ int i , count [128 ] = { 0 }, pat_len ;
10+ int l = 0 , r = 0 , len = 0 ;
11+
1012 for (i = 0 ; s1 [i ] != '\0' ; i ++ ) {
1113 count [s1 [i ]]++ ;
12- pat_len ++ ;
1314 }
15+ pat_len = i ;
1416
15- int l = 0 , r = 0 , len = 0 ;
1617 while (s2 [r ] != '\0' ) {
1718 if (-- count [s2 [r ++ ]] >= 0 ) {
1819 len ++ ;
1920 }
20- while (r - l >= pat_len ) {
21- if (len == pat_len ) {
21+
22+ while (len >= pat_len ) {
23+ if (r - l == pat_len ) {
2224 return true;
2325 }
2426 if (++ count [s2 [l ++ ]] > 0 ) {
You can’t perform that action at this time.
0 commit comments