Go clean solution (Sliding window)
Time complexity: O((n-minSize)(maxSize-minSize)) time, because we have (n-minSize) outer iteration, and in each of them we check max - minSize substrings\nSpace
**Time complexity**: O((n-minSize)*(maxSize-minSize)) time, because we have (n-minSize) outer iteration, and in each of them we check max - minSize substrings\n**Space complexity**: O((n-minSize)*(maxSize-minSize)) space, because in the worst case scenario we\'ll have validSubstringsCount entry for each substring.\n\nIt\'s hard to compute Time and Space complexities in this questions, but I\'d say they are both lower than O(n^2) at least :)\n```\nfunc maxFreq(s string, maxLetters int, minSize int, maxSize int) int {\n\tvalidSubstringsCount := map[string]int{\n\t\t"": 0,\n\t}\n\tmaxValidString := ""\n\n\t// Populate base sliding window\n\tbaseLetterFreqs := make(map[byte]int)\n\tfor i := 0; i < minSize-1; i++ {\n\t\tbaseLetterFreqs[s[i]] += 1\n\t}\n\n\t// Make a copy of base sliding window, then enlarge until it hits maxSize, checking valid substrings\n\tfor i := 0; i < len(s)-minSize+1; i++ {\n\t\tletterFreqs := copyMap(baseLetterFreqs)\n\n\t\tfor j := i + minSize - 1; j < i+maxSize && j < len(s); j++ {\n\t\t\tletterFreqs[s[j]] += 1\n\n\t\t\tif len(letterFreqs) > maxLetters {\n\t\t\t\t// Every other loop iteration will not bring us anything, len(letterFreqs) will only increase\n\t\t\t\tbreak\n\t\t\t}\n\n\t\t\tvalidSubstringsCount[s[i:j+1]] += 1\n\t\t\tif validSubstringsCount[s[i:j+1]] > validSubstringsCount[maxValidString] {\n\t\t\t\tmaxValidString = s[i : j+1]\n\t\t\t}\n\t\t}\n\n\t\t// Slide base sliding window\n\t\tif minSize != 1 {\n\t\t\tbaseLetterFreqs[s[i]] -= 1\n\t\t\tif baseLetterFreqs[s[i]] <= 0 {\n\t\t\t\tdelete(baseLetterFreqs, s[i])\n\t\t\t}\n\n\t\t\tbaseLetterFreqs[s[i+minSize-1]] += 1\n\t\t}\n\t}\n\n\treturn validSubstringsCount[maxValidString]\n}\n\nfunc copyMap(m map[byte]int) map[byte]int {\n\tmCopy := make(map[byte]int, len(m))\n\n\tfor k, v := range m {\n\t\tmCopy[k] = v\n\t}\n\n\treturn mCopy\n}\n```
['Sliding Window', 'Go']