1805. Number of Different Integers in a String LeetCode Solution
In this guide, you will get 1805. Number of Different Integers in a String LeetCode Solution with the best time and space complexity. The solution to Number of Different Integers in a String problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Number of Different Integers in a String solution in C++
- Number of Different Integers in a String solution in Java
- Number of Different Integers in a String solution in Python
- Additional Resources
Problem Statement of Number of Different Integers in a String
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.
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(n)
1805. Number of Different Integers in a String LeetCode Solution in C++
class Solution {
public:
int numDifferentIntegers(string word) {
unordered_set<string> nums;
string curr;
for (const char c : word)
if (isdigit(c)) {
curr += c;
} else if (curr.length() > 0) {
nums.insert(removeLeadingZeros(curr));
curr = "";
}
if (curr.length() > 0)
nums.insert(removeLeadingZeros(curr));
return nums.size();
}
private:
string removeLeadingZeros(const string& s) {
const int index = s.find_first_not_of('0');
return index == string::npos ? "0" : s.substr(index);
}
};
/* code provided by PROGIEZ */
1805. Number of Different Integers in a String LeetCode Solution in Java
class Solution {
public int numDifferentIntegers(String word) {
HashSet<String> nums = new HashSet<>();
StringBuilder sb = new StringBuilder();
for (final char c : word.toCharArray())
if (Character.isDigit(c)) {
sb.append(c);
} else if (sb.length() > 0) {
nums.add(removeLeadingZeros(sb.toString()));
sb = new StringBuilder();
}
if (sb.length() > 0)
nums.add(removeLeadingZeros(sb.toString()));
return nums.size();
}
private String removeLeadingZeros(final String s) {
int index = 0;
while (index < s.length() && s.charAt(index) == '0')
++index;
return index == s.length() ? "0" : s.substring(index);
}
}
// code provided by PROGIEZ
1805. Number of Different Integers in a String LeetCode Solution in Python
class Solution:
def numDifferentIntegers(self, word: str) -> int:
nums = set()
curr = []
for c in word:
if c.isdigit():
curr.append(c)
elif curr:
nums.add(''.join(self._removeLeadingZeros(curr)))
curr = []
if curr:
nums.add(''.join(self._removeLeadingZeros(curr)))
return len(nums)
def _removeLeadingZeros(self, s: str) -> str:
index = next((i for i, c in enumerate(s) if c != '0'), -1)
return ['0'] if index == -1 else s[index:]
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.