Leetcode longest substring without repeating characters Link here
I'm currently learning c++ coming from a python background, so I'll include a solution in python and in c++ for the following problem statement and based on very helpful answers obtained on my previous question I made some improvements in the c++ implementation:

Given a string s, find the length of the longest substring without repeating characters.

Example 1:
Input: s = &quot;abcabcbb&quot;
Output: 3
Example 2:
Input: s = &quot;bbbbb&quot;
Output: 1
I would like to improve speed for both python and c++ implementations, and I need to improve memory consumption in the c++ implementation since I got a very high figure (600+ MB) as well as the reasons for this high consumption (if the figure is accurate), and I would like general suggestions too.
longest_substring.py
def get_longest(s: str):
    possibilities = (s[i:] for i in range(len(s)))
    maximum = 0
    for possibility in possibilities:
        end_idx = maximum
        while end_idx &lt;= len(possibility):
            current_chunk = possibility[0:end_idx]
            end_idx += 1
            if not (current_size := len(current_chunk)) == len(set(current_chunk)):
                break
            maximum = max(current_size, maximum)
    return maximum


if __name__ == '__main__':
    print(f'Longest substring:\n{get_longest(&quot;abcabcbb&quot;)}')

Leetcode stats:

Runtime: 260 ms, faster than 19.36% of Python3 online submissions for Longest Substring Without Repeating Characters.


Memory Usage: 14.4 MB, less than 100.00% of Python3 online submissions for Longest Substring Without Repeating Characters.

longest_substring.h
#ifndef LEETCODE_LONGEST_SUBSTRING_H
#define LEETCODE_LONGEST_SUBSTRING_H

#include &lt;string&gt;

int longest_sub(const std::string &amp;s);
bool check_unique(const std::string &amp;s);

#endif //LEETCODE_LONGEST_SUBSTRING_H

longest_substring.cpp
#include &quot;longest_substring.h&quot;
#include &lt;iostream&gt;
using std::endl;
using std::cout;
using std::string;


bool check_unique(const string &amp;s) {
    for (size_t i = 0; i &lt; s.size() - 1; ++i) {
        for (size_t j = i + 1; j &lt; s.size(); ++j) {
            if (s[i] == s[j])
                return false;
        }
    }
    return true;
}


int longest_sub(const string &amp;s) {
    int maximum = 0;
    for (size_t i = 0; i &lt; s.size(); ++i) {
        const string possibility = s.substr(i);
        auto end_idx = maximum;
        while (end_idx &lt; possibility.size()) {
            const string current_chunk = possibility.substr(0, ++end_idx);
            if (!check_unique(current_chunk))
                break;
            auto current_size = current_chunk.size();
            if (current_size &gt; maximum)
                maximum = current_size;
        }
    }
    return maximum;
}


int main() {
    cout &lt;&lt; &quot;Longest substring: &quot; &lt;&lt; endl;
    cout &lt;&lt; longest_sub(&quot;abcabcbb&quot;);
}

Leetcode stats:

Runtime: 100 ms, faster than 14.88% of C++ online submissions for Longest Substring Without Repeating Characters.


Memory Usage: 604.2 MB, less than 5.02% of C++ online submissions for Longest Substring Without Repeating Characters.
Complexity
Your solution has time complexity \$\mathcal{O}(N^4)\$, which is very bad. There is a \$\mathcal{O}(N)\$ solution for this problem. Consider for example the string:
abcdecfghij

Instead of taking substrings and checking if the substring has duplicates, instead keep track of the last position seen for any possible character. This is basically an array of 256 ints, which you should initialize to -1 to indicate that you've never seen the character before. Then, iterate over the string character by character, and check if the character you look at has already been seen. If not, update its position in the array. So after processing abcde, you will have a = 0, b = 1, c = 2, d = 3, e = 4 and the rest is still -1. Then when you encounter c again, you know you have a duplicate. But instead of starting over from the second character of the string, you should start instead from the character right after the first c, so at position 3. And you know you already have a valid substring up to and including the second c. So then you can continue from there. You continue until you find a character with a recorded position that is equal to or more than the starting position of the current substring. Here is a possible implementation in C++:
#include &lt;array&gt;
#include &lt;utility&gt;

int longest_sub(const std::string &amp;s) {
    std::array&lt;int, 256&gt; last_positions;
    last_positions.fill(-1);

    int min_position = 0;
    int maximum_length = 0;

    for (size_t i = 0; i &lt; s.size(); ++i) {
        int &amp;last_position = last_positions[static_cast&lt;unsigned char&gt;(s[i])];

        if (last_position &gt;= min_position) {
            // We encountered a duplicate
            min_position = last_position + 1;
        }

        maximum_length = std::max(maximum_length, int(i + 1 - min_position));
        last_position = i;
    }

    return maximum_length;
}