Re: IsAnagram
- From: CBFalconer <cbfalconer@xxxxxxxxx>
- Date: Thu, 10 May 2007 02:20:51 -0400
Martin wrote:
I am looking for the best algorithm to check if two words are
anagrams. Here the signature of the method :
bool IsAnagram(char[] wordOne, char[] wordTwo)
Here are the strategies I am thinking of :
- Brute-Force (One loop inside the other one) O(n^2)
- 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.
So I am looking for the most efficient algorithm. Do you have an idea?
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). If one argument comes from a table then
it can be stored in sorted order.
Bear in mind that you are not passing arrays, you are passing
pointers to the first item in the array. So you need to copy the
array first. Assuming they are strings (zero terminated) you will
find the lengths in the process.
--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net
--
Posted via a free Usenet account from http://www.teranews.com
.
- Follow-Ups:
- Re: IsAnagram
- From: Christopher Benson-Manica
- Re: IsAnagram
- From: Richard Bos
- Re: IsAnagram
- References:
- IsAnagram
- From: Martin
- IsAnagram
- Prev by Date: Re: segmentation error
- Next by Date: Re: Dealing with naive malloc() implementations
- Previous by thread: Re: IsAnagram
- Next by thread: Re: IsAnagram
- Index(es):
Relevant Pages
|