mirror of
https://github.com/TheAlgorithms/C
synced 2025-02-16 21:44:16 +03:00
added solution to Leetcode problem 771.Jewels and Stones
This commit is contained in:
parent
3f4599e674
commit
d45ffafcf5
19
leetcode/771.c
Normal file
19
leetcode/771.c
Normal file
@ -0,0 +1,19 @@
|
||||
// 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;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user