C++ O(n) Clean Code without Nested Loops


  • 1
    F
    class Solution {
    public:
        int lengthOfLongestSubstringKDistinct(string s, int k) {
            if (k <= 0) return 0;
            unordered_map<char, int> count_map;
            int i = 0, j = 0, max_len = 0;
            while (j < s.size())
            {
                if (count_map.size() > k)
                {
                    auto iter = count_map.find(s[i]);
                    iter->second --;
                    if (iter->second == 0)
                        count_map.erase(iter);
                    i ++;
                }else{
                    max_len = max(j - i, max_len);
                    count_map[s[j]] ++;
                    j ++;
                }
            }
            if (count_map.size() <= k)
                max_len = max(j - i, max_len);
            return max_len;
        }
    };

Log in to reply
 

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