Largest Substring Without Repeating Char

code

class Solution {
public:
    /**
     * @param s: a string
     * @return: an integer 
     */
    int lengthOfLongestSubstring(string s) {
        // write your code here
        if(s.length() == 0)
        {
            return 0;
        }

        unordered_set<char> hash;

        int r = 0;
        int ret = 0;

        for(int i = 0; i<s.length(); i++)
        {
            while(r < s.length())
            {
                if(hash.find(s[r]) == hash.end())
                {
                    hash.insert(s[r]);
                    ret = max(ret, (int)hash.size());
                    r++;
                }
                else
                {
                    break;
                }
            }
            hash.erase(s[i]);
        }

        return ret;
    }
};

results matching ""

    No results matching ""