Maximum Number of Occurrences of a Substring LeetCode solution in C++
1class Solution {2public:3int maxFreq(string s, int maxLetters, int minSize, int maxSize) {4int i, j, k, ans = 0, res, n = s.length();56string temp;7unordered_map<char, int> st;8unordered_map<string, int> mp;910i = 0, j = 0;11while (i <= n - minSize && j < n) {12temp += s[j];13st[s[j]] += 1;14;15if (temp.length() == minSize) {16if (st.size() <= maxLetters) mp[temp] += 1;17i += 1;18st[temp[0]] -= 1;19if (st[temp[0]] == 0) st.erase(temp[0]);20temp.erase(temp.begin());21}22j += 1;23}24for (auto el : mp) ans = max(ans, el.second);25return ans;26}27};