2

Let assume I have a word

word = abcde

And a score board to show that the position of the characters in the word is correct or not in comparison to my desired output

score = [T, F, F, T, F]

Where T is for correct position, F is for incorrect. So we will have possible output to be acedb and aebdc.
Now given a list of words

words = [abcde, aaaaa, ababa, acedb, bbced]

Based on the scorer above, I would like to find if in the list 'words' exist the correct anagram of word, which is in this case acedb and returns it

output = [acedb]

How can I achieve this with efficient time complexity ? Can I do this in linear time ?

I don't want to use in-built functions, libraries or set/dict (hash tables) as they might screw up my complexity for further tasks so any help would be appriciated

241ringgit
  • 57
  • 5
  • How can you do `O(N^2)` time? If all of the 'False'/ wrong position characters are distinct, the number of solutions is the number of [derangements](https://en.wikipedia.org/wiki/Derangement) of `n` (where `n` is the False character count). This number is approximately `n! / 3`. – kcsquared Mar 23 '22 at 13:44
  • @kcsquared My apologies, I was mistaken that with my process of doing another task which is finding X-1 correct permutation in a list of N word. – 241ringgit Mar 23 '22 at 13:56
  • 1
    No worries, thanks for clearing that up. Also, it is usually not considered good practice to include more than one question in an SO post, as that makes them less useful for future readers. I would recommend asking a separate question for that new task about finding correct anagrams, unless it is basically identical to your first question. – kcsquared Mar 23 '22 at 14:14
  • 1
    Generating all possible anagrams based on the score and filtering a list to determine the correct anagram(s) will differ in time complexity. For the first case, it'd have factorial time, as mentioned above. However, we can determine if a given string matches to a correct anagram in O(N) time - just make sure the correct letters are in the right place and that the count of the jumbled ones matches to that in the original. This approach would require preprocessing he original string though, but this should just be a linear time/space algorithm. – wLui155 Mar 23 '22 at 14:59
  • @kcsquared I've edited it as one so it suits my final goal. Basically in what wLui155 said it's to determine if a given string matches to a correct anagram based on the position score. Thank you for your recommendation – 241ringgit Mar 23 '22 at 21:27
  • @wLui155 For the second approach, I'm not sure if it is O(N) given that if I have a list of length n = N but the length of the words in the list is M where M > N, determining through comparision makes it O(MN) in worst case as I was iterating through every characters of each word and every words in the given list ? Or are you suggesting a better way of comparing without having to go through every words in the list or every value in score ? – 241ringgit Mar 23 '22 at 21:37
  • @wLui155 My comment no longer applies, probably best to ignore that. The question was a bit different when the current answer and my comment were posted. – kcsquared Mar 23 '22 at 21:42

1 Answers1

2

Permute all characters with F and check the string so that we don't introduce any new T's.

Example: word = abcde, score = [T, F, F, T, F]

  1. Find all permutations of characters with F's i.e. b,c,e.
  2. For each permutation compare the string with word so that we don't introduce any new T's and return the list.

For permutation b,e,c: abedc is not valid anagram as it will have score [T,T,F,T,F].

For permutation e,b,c: aebdc is valid anagram as it will have score [T,F,F,T,F]

Let n be the number of F's in score. Time complexity would be O(n! * n).

In worst case, n = length of the word say N. So, O(N! * N).


Not sure how you manage to pull off this in O(N^2). Also, not sure why you think using in-built functions, libraries or set/dict (hash tables) might screw up the complexity. Anyways, hope it helped.

Shridhar R Kulkarni
  • 6,653
  • 3
  • 37
  • 57