My C++ answer using space exchange time.What is the time complexity?who knows?


  • 0
    L

    class Solution {
    public:
    int lengthOfLongestSubstring(string s) {

        if (s.empty())
            return 0;
        string str;
        string::size_type pos=0,index=1;
        string::size_type maxLen = 1;
        
        for (str = s[0];index<s.size();index++)
        {
            if( (pos =str.find_first_of(s[index])) != string::npos)
            {
                str = str.substr(pos+1) + s[index];
            }
            else
            {
                str = str + s[index];
            }
            
            maxLen = maxLen<str.size()?str.size():maxLen;
    
        }
        return maxLen;
        
        
    }
    

    };


Log in to reply
 

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