Number of Different Integers in a String - Problem

You are given a string word that consists of digits and lowercase English letters.

You will replace every non-digit character with a space. For example, "a123bc34d8ef34" will become " 123 34 8 34". Notice that you are left with some integers that are separated by at least one space: "123", "34", "8", and "34".

Return the number of different integers after performing the replacement operations on word.

Two integers are considered different if their decimal representations without any leading zeros are different.

Input & Output

Example 1 — Basic Case
$ Input: word = "a123bc34d8ef34"
Output: 3
💡 Note: After replacing letters with spaces: " 123 34 8 34". The unique numbers are: "123", "34", "8". Note that "34" appears twice but counts as one unique number.
Example 2 — Leading Zeros
$ Input: word = "leet1234code234"
Output: 2
💡 Note: After replacement: " 1234 234". The unique numbers are: "1234" and "234".
Example 3 — Leading Zeros Normalization
$ Input: word = "a1b01c001"
Output: 1
💡 Note: After replacement: " 1 01 001". All three numbers normalize to "1" after removing leading zeros, so there's only 1 unique number.

Constraints

  • 1 ≤ word.length ≤ 1000
  • word consists of digits and lowercase English letters.

Visualization

Tap to expand
Number of Different Integers in StringInput: "a123bc34d8ef34"Extract & NormalizeNumber123Number34Number8Duplicate34Unique Numbers Set{123, 34, 8}Answer3
Understanding the Visualization
1
Input Processing
String with mixed letters and digits
2
Number Extraction
Replace letters, extract and normalize numbers
3
Count Unique
Count distinct normalized integers
Key Takeaway
🎯 Key Insight: Use string processing to extract numbers, normalize them by removing leading zeros, then leverage a hash set for automatic duplicate removal and counting.
Asked in
Google 15 Amazon 12 Microsoft 8
18.5K Views
Medium Frequency
~15 min Avg. Time
428 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