Skip to content

Commit 9491375

Browse files
authored
Added Number of Different Integers in a String.java
1 parent b2072a4 commit 9491375

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int numDifferentIntegers(String word) {
3+
Set<Integer> set = new HashSet<>();
4+
int idx = 0;
5+
int n = word.length();
6+
while (idx < n) {
7+
if (Character.isDigit(word.charAt(idx))) {
8+
int num = 0;
9+
while (idx < n && Character.isDigit(word.charAt(idx))) {
10+
num = num * 10 + Character.getNumericValue(word.charAt(idx++));
11+
}
12+
set.add(num);
13+
}
14+
idx++;
15+
}
16+
return set.size();
17+
}
18+
}

0 commit comments

Comments
 (0)