java string permutations and combinations lookup

后端 未结 6 523
北恋
北恋 2021-01-02 08:35

I\'m writing an Android word app. My code includes a method that would find all combinations of the string and the substrings of a 7 letter string with a minimum of

6条回答
  •  时光说笑
    2021-01-02 09:15

    Well, you can extend your dictionary entities with array letters[] where letters[i] stays for times that i-th letter of alphabet used in this word. It'll take some additional memory, not far much than it is used now.

    Then, for each word which permutations you want to check, you'll need to count number of distinct letters too and then traverse through dictiory with easy comparison procedure. If for all letters for word from dictionary number of occurrences less or equal than for word we are checking - yes, this word can be represented as permutation of substring, otherwise - no.

    Complexity: it'll took O(D * maxLen) for precalculation, and O(max(N, D)) for each query.

提交回复
热议问题