Easy 8 lines C++ O(n)


  • 0
    class Solution {
    public:
        int lengthOfLongestSubstringKDistinct(string s, int k) {
            unordered_map<char, int>m;
            int i = 0, j = 0, cnt = 0, maxlen = 0;
            while(j < s.size()){
                if(m[s[j++]]++ == 0) cnt++;
                if(cnt <= k) maxlen = max(maxlen, j - i);
                while(cnt > k) if(--m[s[i++]] == 0) cnt--;
            }
            return maxlen;
        }
    };
    

Log in to reply
 

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