3581. Count Odd Letters from Number
You are given an integer n perform the following steps:
- Convert each digit of
n into its lowercase English word (e.g., 4 → “four”, 1 → “one”).
- Concatenate those words in the original digit order to form a string
s.
Return the number of distinct characters in s that appear an odd number of times.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| class Solution { public: int countOddLetters(int n) { unordered_map<int,string> numbers = { {0, "zero"}, {1, "one"}, {2, "two"}, {3, "three"}, {4, "four"}, {5, "five"}, {6, "six"}, {7, "seven"}, {8, "eight"}, {9, "nine"} }; int res = 0; while(n) { int x = n % 10; n /= 10; for(auto& ch : numbers[x]) { res ^= 1<<(ch - 'a'); } } return __builtin_popcount(res); } };
|