# [1941. Check if All Characters Have Equal Number of Occurrences](https://leetcode.com/problems/check-if-all-characters-have-equal-number-of-occurrences/description/) ## 1. 題目說明 - **給一串字串,判斷該字串是否為==好字串==** - **好字串定義:** - **字串中出現的所有字符,其出現次數都一樣** ## 2. 解題思路1 - **該題目與統計有關,可以使用 hash 紀錄字母的出現次數** - **統計完成後,找一個字母與其他字串中出現的字母比較出現次數** ### ● 程式碼 ```cpp= bool areOccurrencesEqual(char* s) { int hash[26] = {0}; int index = 0; for (int i = 0; i < strlen(s); i++){ index = s[i] - 'a'; hash[index]++; } for (int i = 0; i < 26; i++) if (hash[i] && hash[index] != hash[i]) return false; return true; } ``` ---
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up