Swimmer

[LeetCode] 771, Jewels and Stones 본문

코딩 문제

[LeetCode] 771, Jewels and Stones

Zach Choi 2023. 2. 14. 07:25

Skill

  • Use hash table to decrease time complexity to under log(n^2)

Others

  • Use string struct member
  • Use ASCII Number of english letter
int gstHashTable[60] = { 0 };

class Solution {
public:
	int numJewelsInStones(string jewels, string stones) {
		memset(&gstHashTable, 0, sizeof(int) * 60);
		int RetVal = 0;

		for (int i = 0; i != stones.size(); ++i)
		{
			gstHashTable[stones[i] - 65]++;
		}

		for (int i = 0; i != jewels.size(); ++i)
		{
			RetVal += gstHashTable[jewels[i] - 65];
		}

		return RetVal;
	}
};
Comments