 <p>By keeping a circular buffer of runes, you can minimise allocations. Also note that reading a new key from a map returns the zero value (which for int is 0), which means the unknown key check in your code is redundant.</p> <pre><code>func Parse(text string, n int) map[string]int { chars := make([]rune, 2 * n) table := make(map[string]int) k := 0 for _, chars[k] = range strings.Join(strings.Fields(text), " ") + " " { chars[n + k] = chars[k] k = (k + 1) % n table[string(chars[k:k+n])]++ } return table } </code></pre>