Java | sliding Window | 0(26*n)
class Solution {\n public int maxFreq(String s, int maxLetters, int minSize, int maxSize) {\n    Map map = new HashMap<>();\n    int maxFrequency = 0;\n\n    \n
class Solution {\n public int maxFreq(String s, int maxLetters, int minSize, int maxSize) {\n    Map<String, Integer> map = new HashMap<>();\n    int maxFrequency = 0;\n\n    \n    for (int size = minSize; size <= maxSize; size++) {\n        Map<Character, Integer> charCount = new HashMap<>();\n        int uniqueChars = 0;\n\n        \n        for (int i = 0; i < size; i++) {\n            char c = s.charAt(i);\n            charCount.put(c, charCount.getOrDefault(c, 0) + 1);\n            if (charCount.get(c) == 1) {\n                uniqueChars++;\n            }\n        }\n\n        if (uniqueChars <= maxLetters) {\n            String substring = s.substring(0, size);\n            map.put(substring, map.getOrDefault(substring, 0) + 1);\n            maxFrequency = Math.max(maxFrequency, map.get(substring));\n        }\n\n        \n        for (int i = size; i < s.length(); i++) {\n            char newChar = s.charAt(i);\n            char oldChar = s.charAt(i - size);\n\n           \n            charCount.put(newChar, charCount.getOrDefault(newChar, 0) + 1);\n            if (charCount.get(newChar) == 1) {\n                uniqueChars++;\n            }\n\n            \n            charCount.put(oldChar, charCount.get(oldChar) - 1);\n            if (charCount.get(oldChar) == 0) {\n                uniqueChars--;\n            }\n\n       \n            if (uniqueChars <= maxLetters) {\n                String substring = s.substring(i - size + 1, i + 1);\n                map.put(substring, map.getOrDefault(substring, 0) + 1);\n                maxFrequency = Math.max(maxFrequency, map.get(substring));\n            }\n        }\n    }\n\n    return maxFrequency;\n  }\n}
[]