text and kit. Treat kit as a set of letters, repeats included. A substring of text qualifies if it holds the whole set: every letter appears in it at least as many times as in kit. Put differently, its letters can be rearranged so that it starts with kit.
Count the non-empty substrings of text that qualify. Substrings at different positions count separately.
text can be up to a million characters long, so aim for a linear-time solution.text="banana", kit="nab" → 4
A qualifying substring must contain the b, so it starts at position 0, and the first n is at position 2. That leaves "ban", "bana", "banan" and "banana".
text="aaaa", kit="aa" → 6
Every substring of length 2 or more works: three of length 2, two of length 3 and one of length 4.
text="abc", kit="abcd" → 0
text="banana", kit="nab"
4
Run checks the first 3 tests in your browser. Submit checks all 71 tests on the server, hidden ones included.