Skip to content

Commit 8a4fecc

Browse files
authored
Create number-of-different-integers-in-a-string.cpp
1 parent 29267fe commit 8a4fecc

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Time: O(n)
2+
// Space: O(n)
3+
4+
class Solution {
5+
public:
6+
int numDifferentIntegers(string word) {
7+
word.push_back(' ');
8+
unordered_set<string> result;
9+
string num;
10+
for (const auto& c : word) {
11+
if (isdigit(c)) {
12+
if (!empty(num) && num[0] == '0') {
13+
num[0] = c;
14+
} else {
15+
num.push_back(c);
16+
}
17+
} else if (!empty(num)) {
18+
result.emplace(move(num));
19+
}
20+
}
21+
return size(result);
22+
}
23+
};

0 commit comments

Comments
 (0)