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.
Example 1:
Input: word = "a123bc34d8ef34" Output: 3 Explanation: The three different integers are "123", "34", and "8". Notice that "34" is only counted once.
Example 2:
Input: word = "leet1234code234" Output: 2
Example 3:
Input: word = "a1b01c001" Output: 1 Explanation: The three integers "1", "01", and "001" all represent the same integer because the leading zeros are ignored when comparing their decimal values.
Constraints:
1 <= word.length <= 1000
word
consists of digits and lowercase English letters.This problem asks us to find the number of unique integers present in a string that may contain both digits and alphabets. The key is to first extract the integers from the string, remove leading zeros, and then count the number of unique integers.
Approach:
Extract Integers: We iterate through the input string word
. When we encounter a digit, we continue to the right until we find a non-digit character, extracting the sequence of digits as a potential integer.
Remove Leading Zeros: Each extracted integer string might have leading zeros. To ensure we treat "01", "001", and "1" as the same integer, we remove leading zeros from each extracted string. This can be efficiently done by using lstrip('0')
in Python or by manually iterating and skipping leading zeros.
Store Unique Integers: We use a set (HashSet in Java, C++, Go, Rust; Set in TypeScript; unordered_set
in C++) to store the unique integers. Sets automatically handle duplicate entries, so we only store each unique integer once.
Return Count: Finally, we return the size of the set, which represents the number of unique integers.
Time Complexity Analysis:
The algorithm iterates through the input string word
once to extract integers. The process of removing leading zeros and inserting into the set takes constant time for each integer. Therefore, the overall time complexity is O(n), where n is the length of the input string.
Space Complexity Analysis:
The space complexity is determined by the size of the set. In the worst-case scenario, all extracted integers are unique, meaning the set will store up to n/2 integers (as integers are likely to be shorter than n/2). Therefore, the space complexity is O(n) in the worst case. In the average case, however, the space complexity could be significantly smaller if the string contains fewer unique integers.
Code Implementation (Python):
class Solution:
def numDifferentIntegers(self, word: str) -> int:
s = set()
num_str = ""
for char in word:
if char.isdigit():
num_str += char
else:
if num_str:
s.add(num_str.lstrip('0'))
num_str = ""
if num_str: # Handle the case where the last character is a digit
s.add(num_str.lstrip('0'))
return len(s)
This Python code is slightly different from the multi-language version provided in the problem statement, mainly in how it handles the extraction and adding of integers to the set. This version avoids double pointers and instead uses a more straightforward approach. Both approaches are valid and have the same time and space complexities. The other language implementations provided earlier follow a similar logic but with the syntax specific to each language.