☘Easy C++ solution || Code with comments at each step || O(n) time O(n) space
\n\n# Complexity\n- Time complexity: O(n)\n\n- Space complexity: O(2*n) => O(n)\n\n# Code\n\nclass Solution {\npublic:\n    //the main observation needed in thi
\n\n# Complexity\n- Time complexity: O(n)\n\n- Space complexity: O(2*n) => O(n)\n\n# Code\n```\nclass Solution {\npublic:\n    //the main observation needed in this question is that we need to check only the\n    //substrings with size minSize, as if a substring of length minSize+x (x>0) is getting repeated y times,\n    //then for sure its substring of size minSize will be getting repeated >=y(at least y) times;\n    int maxFreq(string s, int maxLetters, int minSize, int maxSize) {\n        int n=s.size();\n        unordered_map<char,int> mp;\n        unordered_map<string,int> strs;\n\n        //making the first window:\n        for(int i=0;i<minSize;i++){\n            mp[s[i]]++;\n        }\n        if(mp.size()<=maxLetters)strs[s.substr(0,minSize)]++;\n\n        //sliding the window forward:\n        int i=0 , j=minSize-1;\n        while(i<n-minSize && j<n){\n            mp[s[i]]--;\n            if(mp[s[i]]==0)mp.erase(s[i]);\n            i++;\n            j++;\n            mp[s[j]]++;\n            if(mp.size()<=maxLetters)strs[s.substr(i,j-i+1)]++;\n        }\n\n        //calculating the output:\n        int out=0;\n        for(auto it:strs)out=max(out,it.second);\n        return out;\n    }\n};\n```\n![upvote xd.jpeg](https://assets.leetcode.com/users/images/0d4278b2-3215-43cb-8371-a3efb451b195_1692731514.2707863.jpeg)\n
['Greedy', 'Sliding Window', 'C++']