<p>Given a string <code>s</code>, find the length of the <strong>longest</strong> <span data-keyword="substring-nonempty"><strong>substring</strong></span> without repeating characters.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;abcabcbb&quot;
<strong>Output:</strong> 3
<strong>Explanation:</strong> The answer is &quot;abc&quot;, with the length of 3.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;bbbbb&quot;
<strong>Output:</strong> 1
<strong>Explanation:</strong> The answer is &quot;b&quot;, with the length of 1.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;pwwkew&quot;
<strong>Output:</strong> 3
<strong>Explanation:</strong> The answer is &quot;wke&quot;, with the length of 3.
Notice that the answer must be a substring, &quot;pwke&quot; is a subsequence and not a substring.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>0 &lt;= s.length &lt;= 5 * 10<sup>4</sup></code></li>
	<li><code>s</code> consists of English letters, digits, symbols and spaces.</li>
</ul>

class Solution {
public:
   int lengthOfLongestSubstring(const std::string& s) {
    std::vector<std::string> answers; // To store valid substrings
    std::string subString = ""; // Current substring being built
    
    for (char ch : s) {
        // Find if the current character is already in the current substring
        size_t pos = subString.find(ch);
        if (pos != std::string::npos) {
            // If duplicate found, truncate substring from just after the duplicate
            subString = subString.substr(pos + 1);
        }
        // Append the current character
        subString += ch;
        // Add the current substring to answers
        answers.push_back(subString);
    }
    
    // Find the length of the longest substring in answers
    int length = 0;
    for (const auto& str : answers) {
        length = std::max(length, static_cast<int>(str.size()));
    }
    
    return length;
}
};
39086