Given a word and a text, we need to return the occurrences of anagrams

后端 未结 5 2196
囚心锁ツ
囚心锁ツ 2020-12-17 20:58

Given a word and a text, return the count of the occurrences of anagrams of the word in the text. For eg. word is “for” and the text is “forxxorfxdofr”, anagrams of “for” wi

5条回答
  •  北荒
    北荒 (楼主)
    2020-12-17 21:48

    You can simply look for the character count.

    Say for example that you're looking for anagramms of look. So, you're looking for:

    • a 4 charachter length word,
    • with 1 l, 2 o and 1 k.

    Simply process the first 4 letters, store the counts. Check whether you have a match. Add the next character (increment), remove the old character (decrement). Check again. And so on...

提交回复
热议问题