C++ Implementation using only std::string


  • 4
    D
    class Solution {
    public:
        int lengthOfLongestSubstring(string s) {
            string subs = "";
            int res = 0;
            for (int i=0; i<s.size(); i++) {
                size_t at = subs.find(s[i]);
                if ( at != string::npos) {
                    subs.erase(0, at+1);
                }
                subs += s[i];
                res = subs.size() > res ? subs.size() : res;
            }
            return res;
        }
    };

Log in to reply
 

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