Count Odd Letters from Number - Problem

You are given an integer n. Perform the following steps:

  1. Convert each digit of n into its lowercase English word (e.g., 4 → "four", 1 → "one")
  2. Concatenate those words in the original digit order to form a string s
  3. Return the number of distinct characters in s that appear an odd number of times

For example, if n = 234, we get "twothreefour", then count how many unique characters appear an odd number of times.

Input & Output

Example 1 — Basic Case
$ Input: n = 234
Output: 6
💡 Note: 234 → "two" + "three" + "four" = "twothreefour". Character frequencies: t(2), w(1), o(2), h(1), r(3), e(3), f(1), u(1). Characters with odd frequency: w(1), h(1), r(3), e(3), f(1), u(1) = 6 total
Example 2 — Single Digit
$ Input: n = 5
Output: 4
💡 Note: 5 → "five". Characters: f(1), i(1), v(1), e(1). All 4 characters appear once (odd) = 4 total
Example 3 — Repeated Digits
$ Input: n = 101
Output: 1
💡 Note: 101 → "one" + "zero" + "one" = "onezeroone". Most characters appear even times, only 'z' appears 1 time (odd) = 1 total

Constraints

  • 1 ≤ n ≤ 109

Visualization

Tap to expand
Count Odd Letters from Number INPUT Integer n = 234 2 3 4 "two" "three" "four" Concatenated: "twothreefour" Input: n = 234 digits: [2, 3, 4] ALGORITHM STEPS 1 Convert Digits Map each digit to word 2 Concatenate Join all words together 3 Count with Hash Build frequency map 4 Find Odd Counts Count chars with odd freq Hash Map: t:2 w:1 o:2 h:1 r:2 e:3 f:1 u:1 Odd: w(1), h(1), e(3), f(1), u(1) Wait... recounting needed FINAL RESULT String: "twothreefour" Character Frequencies: t: 2 (even) w: 1 (ODD) o: 2 (even) h: 1 (ODD) r: 2 (even) e: 3 (ODD) f: 1 (ODD) u: 1 (ODD) wait... Output 4 4 distinct chars with odd frequency count Key Insight: Use a hash map to count character frequencies in O(n) time. A character appears an odd number of times if count % 2 == 1. The hash approach gives O(1) lookup for each character, making the overall solution efficient with O(n) time complexity where n is the length of the concatenated string. TutorialsPoint - Count Odd Letters from Number | Hash Approach
Asked in
Google 25 Meta 18 Amazon 15
23.0K Views
Medium Frequency
~15 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen