-
Notifications
You must be signed in to change notification settings - Fork 1
/
NumberOfDifferentIntegersInAString.java
41 lines (35 loc) · 1.19 KB
/
NumberOfDifferentIntegersInAString.java
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
35
36
37
38
39
40
41
package com.smlnskgmail.jaman.leetcodejava.easy;
import java.util.HashSet;
import java.util.Set;
// https://leetcode.com/problems/number-of-different-integers-in-a-string/
public class NumberOfDifferentIntegersInAString {
private final String input;
public NumberOfDifferentIntegersInAString(String input) {
this.input = input;
}
public int solution() {
Set<String> uniq = new HashSet<>();
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
if (Character.isDigit(c)) {
boolean leadingZero = c == '0';
StringBuilder num = new StringBuilder();
while (Character.isDigit(c)) {
if (!leadingZero) {
num.append(c);
} else if (c != '0') {
leadingZero = false;
num.append(c);
}
i++;
if (i == input.length()) {
break;
}
c = input.charAt(i);
}
uniq.add(num.toString());
}
}
return uniq.size();
}
}