I have been working on a practical situation wherein I require an algorithm, have made a generic problem out of that. Considering there are are Two Arrays :-
Source[10] = {‘a’, ‘v’, ‘l’, ‘r’, ‘p’, ‘s’, ‘x’, ‘d’, ‘q’, ‘o’ , ‘g’, ‘m’}
Target[N] = {‘a’, ‘v’, ‘l’, ‘r’, ‘p’, ‘s’, ‘x’, ‘d’, ‘q’, ‘o’ , ‘g’, ‘m’,a’, ‘v’, ‘l’, ‘r’, ‘p’,a’, ‘v’, ‘l’, ‘r’, ‘p’,a’,
‘v’, ‘l’, ‘r’, ‘p’,a’, ‘v’, ‘l’, ‘r’, ‘p’,a’, ‘v’, ‘l’, ‘r’, ‘p’,a’, ‘v’, ‘l’, ‘r’, ‘p’,a’, ‘v’, ‘l’, ‘r’, ‘p’,a’, ‘v’,
‘l’, ‘r’, ‘p’,a’, ‘v’, ‘l’, ‘r’, ‘p’, …. }
We need to have an efficient algorithm to find the frequency of occurrences of characters from Source in Target.
I have thought of hashing the complete Target list and then iterate through the Source and do the lookup in the hashed list. Can people comment/validate the approach.
If your character set is reasonably limited, you can use character codes as indexes into an array of counts. Let’s say you have 16-bit characters. You can do this:
With the array of
countsin hand, you can easily find the frequency by looking up a code from theSourcein thecountsarray.This solution is asymptotically as fast as it could possibly get, but it may not be the most memory-efficient one.