Solved countSubstrings problem