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

前端 未结 3 1882
暗喜
暗喜 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:34

    You can generate a two dimensional array which will contain the number of times each character appears in each position (0-3). For example, arr[1,3] will contain the number of times the digit/character 1 appears in the last position.

    Then for each string s, go over all characters in the string. The ones which appear only once in that position according to the array are the unique characters for that string. In other words, if arr[s[i], i]==1 Then string s is unique in position i.

    This will give you the solution in linear time, while the algorithm you gave will take quadratic time.

提交回复
热议问题