Conceptual error. Should use permutations instead of substrings. Fixed