You are given an integer n perform the following steps:
n into its lowercase English word (e.g., 4 → "four", 1 → "one").s.Return the number of distinct characters in s that appear an odd number of times.
Example 1:
Input: n = 41
Output: 5
Explanation:
41 → "fourone"
Characters with odd frequencies: 'f', 'u', 'r', 'n', 'e'. Thus, the answer is 5.
Example 2:
Input: n = 20
Output: 5
Explanation:
20 → "twozero"
Characters with odd frequencies: 't', 'w', 'z', 'e', 'r'. Thus, the answer is 5.
Constraints:
1 <= n <= 109We can convert each number into its corresponding English word, then count the frequency of each letter. Since the number of letters is limited, we can use an integer mask to represent the occurrence of each letter. Specifically, we can map each letter to a binary bit of the integer. If a letter appears an odd number of times, the corresponding binary bit is 1; otherwise, it's 0. Finally, we only need to count the number of bits that are 1 in mask, which is the answer.
The time complexity is O(log n), where n is the input integer. And the space complexity is O(1).
Java
C++
Go
TypeScript
Rust
Practice Count Odd Letters from Number with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor