771. Jewels and Stones

class Solution { public: int numJewelsInStones(string J, string S) { unordered_map<char, int> m; for (auto c : J) m[c] = 0; for (auto s : S){ if (m.find(s) != m.end()) // found this key m[s]++; } int count = 0; for (auto it: m){ if (it.second > 0) count += it.second; } return count; } };
Naive solution: use a hash table.

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.