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 ofdifferent integers after performing the replacement operations on _word.
Two integers are considered different if their decimal representations
without any leading zeros are different.
Input: word ="a _123_ bc _34_ d _8_ ef _34_ " Output:3 Explanation: The three different integers are "123","34", and "8". Notice that "34"is only counted once.
Input: word ="a _1_ b _01_ c _001_ " 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.
#include<string>#include<set>usingnamespace std;
classSolution {
public:int numDifferentIntegers(string word) {
set<string> s;
int n = word.size();
for (int i =0; i < n;) {
if (isdigit(word[i])) {
int j = i;
while (j < n && isdigit(word[j])) ++j;
string num = word.substr(i, j-i);
while (num.size() >1&& num[0] =='0') num.erase(0,1);
s.insert(num);
i = j;
} else++i;
}
return s.size();
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import"unicode"funcnumDifferentIntegers(wordstring) int {
s:=map[string]struct{}{}
n:= len(word)
fori:=0; i < n; {
ifunicode.IsDigit(rune(word[i])) {
j:=iforj < n&&unicode.IsDigit(rune(word[j])) { j++ }
num:=word[i:j]
for len(num) > 1&&num[0] =='0' { num = num[1:] }
s[num] = struct{}{}
i = j } else { i++ }
}
return len(s)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.util.*;
classSolution {
publicintnumDifferentIntegers(String word) {
Set<String> set =new HashSet<>();
int n = word.length();
for (int i = 0; i < n;) {
if (Character.isDigit(word.charAt(i))) {
int j = i;
while (j < n && Character.isDigit(word.charAt(j))) j++;
String num = word.substring(i, j);
while (num.length() > 1 && num.charAt(0) =='0') num = num.substring(1);
set.add(num);
i = j;
} else i++;
}
return set.size();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
classSolution {
funnumDifferentIntegers(word: String): Int {
val set = mutableSetOf<String>()
var i = 0while (i < word.length) {
if (word[i].isDigit()) {
var j = i
while (j < word.length && word[j].isDigit()) j++var num = word.substring(i, j)
while (num.length > 1&& num[0] =='0') num = num.substring(1)
set.add(num)
i = j
} else i++ }
returnset.size
}
}
1
2
3
4
5
classSolution:
defnumDifferentIntegers(self, word: str) -> int:
import re
nums = re.findall(r'\d+', word)
return len(set(num.lstrip('0') or'0'for num in nums))
use std::collections::HashSet;
impl Solution {
pubfnnum_different_integers(word: String) -> i32 {
letmut set = HashSet::new();
letmut i =0;
let n = word.len();
let bytes = word.as_bytes();
while i < n {
if bytes[i].is_ascii_digit() {
letmut j = i;
while j < n && bytes[j].is_ascii_digit() { j +=1; }
letmut num =&word[i..j];
while num.len() >1&& num.starts_with('0') { num =&num[1..]; }
set.insert(num.to_string());
i = j;
} else { i +=1; }
}
set.len() asi32 }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
functionnumDifferentIntegers(word: string):number {
constset=newSet<string>();
leti=0;
while (i<word.length) {
if (/[0-9]/.test(word[i])) {
letj=i;
while (j<word.length&&/[0-9]/.test(word[j])) j++;
letnum=word.slice(i, j);
while (num.length>1&&num[0] ==='0') num=num.slice(1);
set.add(num);
i=j;
} elsei++;
}
returnset.size;
}