Re: IsAnagram
- From: rlb@xxxxxxxxxxxxxxxxxxxxxx (Richard Bos)
- Date: Thu, 10 May 2007 08:15:10 GMT
CBFalconer <cbfalconer@xxxxxxxxx> wrote:
Martin wrote:
I am looking for the best algorithm to check if two words are
anagrams. Here the signature of the method :
- Sort both words and compare both arrays. O(n^2) considering that I
use BubbleSort and words are both short (less than 30 characters).
- Find a way to create a signature for each word (e.g. A = 1, B = 2,
etc.) However, I can have a problem here... The sum for both words
can be the same with different letters.
Sort both sounds like the most efficient. You should probably use
a simple sort such as shell sort, which is much faster than bubble,
and is better than O(N*N).
For a max of 30 letters? Insertion or selection. At that size it doesn't
pay to be complicated.
Alternatively, do create a signature, with unsigned long longs, using
not addition but multiplication. If the signatures don't match, they're
not anagrams. Since this is the most usual case, you win a little. If
they _do_ match, fall back to the sorting solution. Yes, the signature
may wrap around for large words, but that's not going to matter because
you need the final check anyway, and all large anagrams are going to
wrap around to the same signature. You do need _unsigned_ integers for
this, mind you. And do check whether it's actually faster or not.
And yet another: if you're going to do great numbers of these checks,
consider using a database. For each word you're being asked to check,
check to see if it's already in the DB. If not, sort it _once_, then add
it to the DB - next time you don't need to sort, you can just fetch the
sorted version from the DB. If you hit two words you've already seen
before, all you need is two DB checks and a single compare. Drawback:
for this to save you anything, you really do need _lots_ of checks, and
a good database.
Richard
.
- Follow-Ups:
- Re: IsAnagram
- From: David Mathog
- Re: IsAnagram
- References:
- IsAnagram
- From: Martin
- Re: IsAnagram
- From: CBFalconer
- IsAnagram
- Prev by Date: Re: access of memory beyond allocation, not causing segmentation fault...
- Next by Date: Re: How to improve C skill?
- Previous by thread: Re: IsAnagram
- Next by thread: Re: IsAnagram
- Index(es):
Relevant Pages
|