Skip to content

Commit

Permalink
Create substrings-of-size-three-with-distinct-characters.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
kamyu104 authored May 30, 2021
1 parent 1fba8c5 commit 1423d09
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions C++/substrings-of-size-three-with-distinct-characters.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Time: O(n)
// Space: O(1)

class Solution {
public:
int countGoodSubstrings(string s) {
static const int K = 3;

int result = 0;
unordered_map<int, int> count;
for (int i = 0; i < size(s); ++i) {
if (i >= K) {
if (--count[s[i - K]] == 0) {
count.erase(s[i - K]);
}
}
++count[s[i]];
if (size(count) == K) {
++result;
}
}
return result;
}
};

0 comments on commit 1423d09

Please sign in to comment.