mirror of
https://github.com/TheAlgorithms/C
synced 2025-04-22 05:06:12 +03:00

The for loop utilized in 283 was improperly structured as 'start' was no declared as the value to index. Also, make the other cases more readable. Signed-off-by: RJ Trujillo <certifiedblyndguy@gmail.com>
20 lines
472 B
C
20 lines
472 B
C
// for strlen()
|
|
#include <string.h>
|
|
|
|
int numJewelsInStones(char * j, char * s) {
|
|
// as strlen is O(n), store it once rather than using it in for loop
|
|
int cnt[500], lens = strlen(s), lenj = strlen(j), sol = 0;
|
|
memset(cnt, 0, sizeof(cnt));
|
|
|
|
// lookup to know which character occurs in j
|
|
for (int i = 0; i < lenj; i++)
|
|
cnt[j[i]]++;
|
|
|
|
// count the characters in s
|
|
for (int i = 0; i < lens; i++)
|
|
sol += cnt[s[i]];
|
|
|
|
return sol;
|
|
}
|
|
|