Problem: 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.


Python solution:
import math
import itertools
import bisect
import collections
import string
import heapq
import functools
import sortedcontainers
from typing import List, Dict, Tuple, Iterator

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)
    if index == -1:
      return ['0']
    else:
      return s[index:]


Java solution:
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);
  }
}


C++ solution:
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);
  }
};