[C++/Java] 40 ms, HashMap + bitset, 11 lines of code
The maxSize is useless since the bigger substring includes a smaller one. \nSo we can safely check just for a min allowed length.\n\nBitset is used to check the
The maxSize is useless since the bigger substring includes a smaller one. \nSo we can safely check just for a min allowed length.\n\nBitset is used to check the number of distinct chars.\nHashMap quickly counts substrings.\n\nC++, 44 ms and beats 100%\n```cpp\nclass Solution {\npublic:\n    int maxFreq(string s, int maxLetters, int minSize, int maxSize)  {\n        int res=0, len=s.size();\n        unordered_map<string, int> m; // substring -> count\n        bitset<26> b; // distinct letters\n        for(int l=0; l<len-minSize+1; l++) {\n            string sub=s.substr(l,minSize);\n            b.reset();\n            for(char c:sub) b.set(c-\'a\');\n            if(b.count()>maxLetters) continue;\n            res=max(res,++m[sub]); // count matching substrings\n        }\n        return res;\n    }\n};\n```\n\nJava, 40 ms\n```java\nclass Solution {\n    public int maxFreq(String s, int maxLetters, int minSize, int maxSize) {\n        int res=0, len=s.length();\n        Map<String, Integer> m = new HashMap<>(); // substring -> count\n        Integer b; // distinct letters\n        for(int l=0; l<len-minSize+1; l++) {\n            String sub=s.substring(l,l+minSize);\n            b=0;\n            for(char c:sub.toCharArray()) b |= 1 << c-\'a\';\n            if(Integer.bitCount(b)>maxLetters) continue;\n            \n            // count matching substrings\n            if(!m.containsKey(sub)){\n                m.put(sub, 1);\n                res = Math.max(res,1);\n            }else{\n                int cur = m.get(sub);\n                m.put(sub, cur + 1);\n                res = Math.max(res, cur+1);\n            }\n        }\n        return res;\n    }\n}\n```\n
['C', 'Java']