[JavaScript] Easy to understand - O(n) with Bit
The strategy is simple. We try to find out all the substrings which follow the rules in description. Use a map to store and counting. Finally, we get the result
The strategy is simple. We try to find out all the substrings which follow the rules in description. Use a map to store and counting. Finally, we get the result.\n\nAccording to the strategy, we have this code with 112ms.\n\n```js\nconst maxFreq = (s, maxLetters, minSize, maxSize) => {\n  const substrMap = new Map();\n  let max = 0;\n  outer:\n  for (let i = 0; i <= s.length - minSize; ++i) {\n    const substr = s.substr(i, minSize);\n    const letterSet = new Set();\n    for (const char of substr) {\n      letterSet.add(char);\n      if (letterSet.size > maxLetters) continue outer;\n    }\n    const count = substrMap.has(substr) ? substrMap.get(substr) + 1 : 1;\n    substrMap.set(substr, count);\n    count > max && (max = count);\n  }\n  return max;\n};\n```\n\nAnd then, I see the `Bit Manipulation` tag in the description. I find out there\'s something more I can do. Right now, we use a `Set` to check the number of different letters for the substring. But the `maxLetters` is between [1, 26] which means we could use a 32-bit integer to store and check the substring.\n\nHere\'s the code with 68ms beats 100% right now.\n\n```js\nconst maxFreq = (s, maxLetters, minSize, maxSize) => {\n  const substrMap = new Map();\n  let max = 0;\n  outer:\n  for (let i = 0; i <= s.length - minSize; ++i) {\n    const substr = s.substr(i, minSize);\n    let flag = len = 0;\n    for (let i = 0; i < substr.length; ++i) {\n      const inc = 1 << (substr.charCodeAt(i) - 97);\n      if ((flag & inc) === 0 && ++len > maxLetters) continue outer;\n      flag |= inc;\n    }\n    const count = substrMap.has(substr) ? substrMap.get(substr) + 1 : 1;\n    substrMap.set(substr, count);\n    count > max && (max = count);\n  }\n  return max;\n};\n```\n\nPS: If you don\'t like the label using in the 2 pieces of code, you could replace it with a condition expression easily.
['JavaScript']