Skip to content

Commit e193529

Browse files
authored
Merge pull request ByteByteGoHq#19 from ongshunping/cpp-solutions-sliding-windows
Add C++ solutions for Chapter 5 (Sliding Windows)
2 parents 4ecfaf6 + fc208e2 commit e193529

File tree

4 files changed

+123
-0
lines changed

4 files changed

+123
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <string>
2+
#include <unordered_set>
3+
#include <algorithm>
4+
5+
int longestSubstringWithUniqueChars(std::string s) {
6+
int maxLen = 0;
7+
std::unordered_set<char> hashSet;
8+
int left = 0;
9+
int right = 0;
10+
while (right < s.length()) {
11+
// If we encounter a duplicate character in the window, shrink
12+
// the window until it's no longer a duplicate.
13+
while (hashSet.find(s[right]) != hashSet.end()) {
14+
hashSet.erase(s[left]);
15+
left++;
16+
}
17+
// Once there are no more duplicates in the window, update
18+
// 'maxLen' if the current window is larger.
19+
maxLen = std::max(maxLen, right - left + 1);
20+
hashSet.insert(s[right]);
21+
// Expand the window.
22+
right++;
23+
}
24+
return maxLen;
25+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <string>
2+
#include <unordered_map>
3+
#include <algorithm>
4+
5+
int longestSubstringWithUniqueCharsOptimized(std::string s) {
6+
int maxLen = 0;
7+
std::unordered_map<char, int> prevIndexes;
8+
int left = 0;
9+
int right = 0;
10+
while (right < s.length()) {
11+
// If a previous index of the current character is present
12+
// in the current window, it's a duplicate character in the
13+
// window.
14+
if (prevIndexes.find(s[right]) != prevIndexes.end() && prevIndexes[s[right]] >= left) {
15+
// Shrink the window to exclude the previous occurrence
16+
// of this character.
17+
left = prevIndexes[s[right]] + 1;
18+
}
19+
// Update 'maxLen' if the current window is larger.
20+
maxLen = std::max(maxLen, right - left + 1);
21+
prevIndexes[s[right]] = right;
22+
// Expand the window.
23+
right++;
24+
}
25+
return maxLen;
26+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <string>
2+
#include <unordered_map>
3+
#include <algorithm>
4+
5+
int longestUniformSubstringAfterReplacements(std::string s, int k) {
6+
std::unordered_map<char, int> freqs;
7+
int highestFreq = 0;
8+
int maxLen = 0;
9+
int left = 0;
10+
int right = 0;
11+
while (right < s.length()) {
12+
// Update the frequency of the character at the right pointer
13+
// and the highest frequency for the current window.
14+
freqs[s[right]] = freqs[s[right]] + 1;
15+
highestFreq = std::max(highestFreq, freqs[s[right]]);
16+
// Calculate replacements needed for the current window.
17+
int numCharsToReplace = (right - left + 1) - highestFreq;
18+
// Slide the window if the number of replacements needed exceeds
19+
// 'k'. The right pointer always gets advanced, so we just need
20+
// to advance 'left'.
21+
if (numCharsToReplace > k) {
22+
// Remove the character at the left pointer from the hash map
23+
// before advancing the left pointer.
24+
freqs[s[left]] = freqs[s[left]] - 1;
25+
left++;
26+
}
27+
// Since the length of the current window increases or stays the
28+
// same, assign the length of the current window to 'maxLen'.
29+
maxLen = right - left + 1;
30+
// Expand the window.
31+
right++;
32+
}
33+
return maxLen;
34+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <string>
2+
#include <vector>
3+
4+
int substringAnagrams(std::string s, std::string t) {
5+
int lenS = s.length();
6+
int lenT = t.length();
7+
if (lenT > lenS) {
8+
return 0;
9+
}
10+
int count = 0;
11+
std::vector<int> expectedFreqs(26, 0);
12+
std::vector<int> windowFreqs(26, 0);
13+
// Populate 'expectedFreqs' with the characters in string 't'.
14+
for (char c : t) {
15+
expectedFreqs[c - 'a'] += 1;
16+
}
17+
int left = 0;
18+
int right = 0;
19+
while (right < lenS) {
20+
// Add the character at the right pointer to 'windowFreqs'
21+
// before sliding the window.
22+
windowFreqs[s[right] - 'a'] += 1;
23+
// If the window has reached the expected fixed length, we
24+
// advance the left pointer as well as the right pointer to
25+
// slide the window.
26+
if (right - left + 1 == lenT) {
27+
if (windowFreqs == expectedFreqs) {
28+
count += 1;
29+
}
30+
// Remove the character at the left pointer from
31+
// 'windowFreqs' before advancing the left pointer.
32+
windowFreqs[s[left] - 'a'] -= 1;
33+
left += 1;
34+
}
35+
right += 1;
36+
}
37+
return count;
38+
}

0 commit comments

Comments
 (0)