My c++ solution


  • 0
    -

    int lengthOfLongestSubstring(string s) {
    auto size = s.size();
    if (size == 0) return 0;
    int maxLength = 1; string maxString{ s[0] };
    for (int i = 1; i < size ; i++) {
    auto index = maxString.find(s[i]);
    if ( index == -1 )
    maxString += {s[i]};
    else
    maxString = maxString.substr( index + 1, maxString.size()) + s[i];

    	maxLength = max(maxLength, (int)maxString.size());	
    }
    
    return maxLength;
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.