LeetCode 76: Minimum Window Substring I'm posting an sliding window problem of LeetCode (Minimum Window Substring) with C++. If you have time and would like to review the code, please do so, I appreciate that.
On LeetCode, we are only allowed to change the variable names and brute force algorithms are discouraged, usually fail with TLE (Time Limit Error) or MLE (Memory Limit Error).
Problem

Given a string base_string and a string target, find the minimum window
in base_string which will contain all the characters in target in
complexity O(n).
Example:
Input: base_string = &quot;ADOBECODEBANC&quot;, target = &quot;ABC&quot; Output: &quot;BANC&quot;
Note:
If there is no such window in base_string that covers all characters in
target, return the empty string &quot;&quot;. If there is such window, you are
guaranteed that there will always be only one unique minimum window in
base_string.

C++
class Solution {
public:
    string minWindow(string base_string, string target) {
        vector&lt;int&gt; count_map(128, 0);

        for (auto character : target)
            count_map[character]++;

        int left = 0, right = 0;
        int min_left = 0, min_right = INT_MAX;
        int target_length = target.size();

        while (right &lt; base_string.size()) {
            if (count_map[base_string[right++]]-- &gt; 0)
                target_length--;

            while (target_length == 0) {
                if (right - left &lt; min_right)
                    min_right = right - (min_left = left);

                if (count_map[base_string[left++]]++ == 0)
                    target_length++;

            }
        }

        return min_right == INT_MAX ? &quot;&quot; : base_string.substr(min_left, min_right);
    }
};


Reference

Question

Solution

Discussion
In the following code, I tried two modifications.

I slightly modified the way tests are performed, in order to minimize them a little bit

I used iterators, like this :auto p_str = base_string.begin() + right;


with the idea to avoid a redirection here:
count_map[*p_str++]--;

instead of
count_map[base_string[right++]]--;

At the end, we cannot be sure how much the speed has been improved. A benchmark is needed (by LeetCode?). We can only be sure that the code is more difficult to read!
#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;vector&gt;
#include &lt;cassert&gt;


std::string minWindow(std::string base_string, std::string target) {
    std::vector&lt;int&gt; count_map(128, 0);
    int n = base_string.length();

    for (auto character : target)
        count_map[character]++;

    int left = 0, right = 0;
    int min_left = 0, min_right = n+1;
    int target_length = target.size();

    while (right &lt; n) {
        auto p_str = base_string.begin() + right;
        while (target_length &gt; 0 &amp;&amp; p_str != base_string.end()) {
            if (count_map[*p_str++]-- &gt; 0)
                target_length--;
        }
        right = p_str - base_string.begin();
        
        if (target_length == 0 &amp;&amp; right - left &lt; min_right)
                min_right = right - left;

        p_str = base_string.begin() + left;
        while (target_length == 0) {
            if (count_map[*p_str++]++ == 0) {
                left = p_str - base_string.begin() - 1;
                if (right - left &lt; min_right)
                    min_right = right - (min_left = left);
                target_length++;
            }
        }
        left = p_str - base_string.begin();
    }
    if (target_length == 0 &amp;&amp; (right - left &lt; min_right))
        min_right = right - (min_left = left);
            
    return min_right == n ? &quot;&quot; : base_string.substr(min_left, min_right);
}


int main() {
    std::string s = &quot;XXXABYYCTTTABYC&quot;;
    std::string target = &quot;CBA&quot;;
    std::cout &lt;&lt; minWindow (s, target) &lt;&lt; &quot;\n&quot;;
    
}