반응형
https://leetcode.com/problems/jewels-and-stones/
Jewels and Stones - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
It can be solved using the Hash. Put all S string's char as a key and count integer as a value. ('count integer' is how to calculate the n-times per each char as an integer). ex) "aaa" => hash['a'] = 3 (I have 3 'a's). And then Based on that Hash I made, Loop the J string. while looping, add the value if n'th char in the J string find the pair of key and value.
int numJewelsInStones(string J, string S) {
unordered_map<char, int> check;
int answer = 0;
for(int i = 0; i < S.size(); i++){ // O(n)
check[S[i]]+=1;
}
for(int j = 0; j < J.size(); j++){ // O(n)
answer += check[J[j]];
}
return answer;
}
Time Complexity: O(n)
반응형
'알고리즘_개념 및 문제풀이 ' 카테고리의 다른 글
https://leetcode.com/problems/4sum/ (0) | 2019.09.28 |
---|---|
Leetcode/count_primes/Hash/리트코드 (0) | 2019.09.28 |
백준온라인저지(BOJ)/#9613//GCD(최대공약수)/C++/유클리드 호제법(Euclidean Algorithm) (0) | 2019.08.04 |
완전 탐색- 재귀함수를 이용한 완전 탐색(기본 개념) (0) | 2019.06.07 |
프로그래머스/숫자게임/2018서머코딩기출/ (0) | 2019.05.08 |