|Think about Loose Coupling|
searching problemby baxy77bax (Chaplain)
|on Jan 18, 2013 at 10:17 UTC||Need Help??|
baxy77bax has asked for the
wisdom of the Perl Monks concerning the following question:
I have a little problem here. I also have a solution, but maybe someone has a better one. So the problem is as follows: I have a list of words like :
and i need to find out if there is a word which reverse can be matched to any other word in my set. Example:
The easiest solution here would be to create a reverse of every word, join them to the original set, label them , sort them (lex. of course). That way 'ymam' and 'ymamaa' would be one next to another and since I have labelled them I can say in O(c) (c=constant) time if there exists a match to the reverse of 'mamy' or not.
Does anyone have a better solution to this ??
Update: Thnx chordoba that is true, but i missed out one simple detail. These sets are huge. Therefore regex-ing would take much longer then my sorting suggestion. But yes you are correct (++) Thnx
Update : Yes only from the start ! :) That is also a good solution , but trie's are a bit memory heavy wouldn't you say. so that is why I thought sorting is the simplest one plus building a trie takes in practice much longer then sorting. Wouldn't you say?