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
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)
18
18
int i , j , count [256 ] = { 0 };
19
19
int slen = strlen (s );
20
20
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
+
21
27
for (i = 0 ; i < tlen ; i ++ ) {
22
28
count [t [i ]]++ ;
23
29
}
24
30
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 ) {
32
33
/* pattern found */
33
- chars_to_meet -- ;
34
+ chars_to_meet ++ ;
34
35
}
35
36
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 ;
40
41
}
41
42
42
43
/* 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 -- ;
46
46
}
47
47
}
48
48
}
Original file line number Diff line number Diff line change 5
5
/**
6
6
* Note: The returned array must be malloced, assume caller calls free().
7
7
*/
8
- int * findAnagrams (char * s , char * p , int * returnSize ){
8
+ int * findAnagrams (char * s , char * p , int * returnSize )
9
+ {
9
10
* returnSize = 0 ;
10
11
int * res = malloc (11000 * sizeof (int ));
11
12
int i , pat_len = 0 ;
12
13
int count [128 ] = { 0 };
14
+ int l = 0 , r = 0 , len = 0 ;
15
+
13
16
for (i = 0 ; p [i ] != '\0' ; i ++ ) {
14
17
count [p [i ]]++ ;
15
18
}
16
19
pat_len = i ;
17
20
18
- int l = 0 , r = 0 , len = 0 ;
19
21
while (s [r ] != '\0' ) {
20
22
if (-- count [s [r ++ ]] >= 0 ) {
21
23
len ++ ;
22
24
}
23
- if (r - l >= pat_len ) {
24
- if (len == pat_len ) {
25
+
26
+ while (len >= pat_len ) {
27
+ if (r - l == pat_len ) {
25
28
res [(* returnSize )++ ] = l ;
26
29
}
27
30
if (++ count [s [l ++ ]] > 0 ) {
Original file line number Diff line number Diff line change @@ -13,6 +13,9 @@ int longestPalindromeSubseq(char * s)
13
13
int i , j , k ;
14
14
int len = strlen (s );
15
15
int * * dp = malloc (len * sizeof (int * ));
16
+
17
+ /* The dp array indicates the length of palindrome subsequence of
18
+ * nums[i...j] */
16
19
for (i = 0 ; i < len ; i ++ ) {
17
20
dp [i ] = malloc (len * sizeof (int ));
18
21
memset (dp [i ], 0 , len * sizeof (int ));
Original file line number Diff line number Diff line change 6
6
7
7
bool checkInclusion (char * s1 , char * s2 )
8
8
{
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
+
10
12
for (i = 0 ; s1 [i ] != '\0' ; i ++ ) {
11
13
count [s1 [i ]]++ ;
12
- pat_len ++ ;
13
14
}
15
+ pat_len = i ;
14
16
15
- int l = 0 , r = 0 , len = 0 ;
16
17
while (s2 [r ] != '\0' ) {
17
18
if (-- count [s2 [r ++ ]] >= 0 ) {
18
19
len ++ ;
19
20
}
20
- while (r - l >= pat_len ) {
21
- if (len == pat_len ) {
21
+
22
+ while (len >= pat_len ) {
23
+ if (r - l == pat_len ) {
22
24
return true;
23
25
}
24
26
if (++ count [s2 [l ++ ]] > 0 ) {
You can’t perform that action at this time.
0 commit comments