-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberOfDiffIntegers.java
More file actions
34 lines (32 loc) · 1.12 KB
/
NumberOfDiffIntegers.java
File metadata and controls
34 lines (32 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//1805. Number of Different Integers in a String
class Solution {
public int numDifferentIntegers(String word) {
Set<String> set = new HashSet<>();
int n = word.length();
for(int i=0;i<n;i++){
if(Character.isDigit(word.charAt(i))){
while(i<n && word.charAt(i) == '0') ++i;
int startIndex = i;
while(startIndex<n && Character.isDigit(word.charAt(startIndex))) ++startIndex;
set.add(word.substring(i,startIndex));
i = startIndex;
}
}
return set.size();
}
}
// class Solution {
// public int numDifferentIntegers(String word) {
// String num = "";
// Set<String> set = new HashSet<>();
// for(int i=0;i<word.length();i++){
// if(Character.isDigit(word.charAt(i))) num += word.charAt(i);
// else{
// if(num.length() > 0) set.add(num.replaceFirst("^0+", ""));
// num = "";
// }
// }
// if(num.length() > 0) set.add(num.replaceFirst("^0+", ""));
// return set.size();
// }
// }