Finding pixels that make an image unique within a list, can you improve on brute force?

前端 未结 3 1837
暗喜
暗喜 2021-02-06 11:05

Suppose I have a list of strings where each string is

  • exactly 4 characters long and
  • unique within the list.

For each of these strings I wan

3条回答
  •  孤独总比滥情好
    2021-02-06 11:38

    This problem can be solved by trie, or prefix tree.

    See Trie - Wikipedia, the free encyclopedia

    For the 3 strings in your example:

    abcd
    abcc
    bbcb
    

    will be turned into a trie tree (where ^ denotes the root of the tree):

    ^--a-b-c-d
     \      \
      \      c
       \
        b-b-c-b
    

    The path to the node where it branch off are the common prefix. The node after the last branch point is what makes a particular string unique. In this case, they are d, c, b.

    I assume the order of string is not important for you, that you compares all strings to find the uniqueness, not just the neighboring string.

    The complexity should be O(n x m). But this will probably affected by the domain of the characters in your string.

提交回复
热议问题