Suppose I have a list of strings where each string is
For each of these strings I wan
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.