Check if all substrings are anagrams of words | Google Interview Question