# Check if Two Strings are Anagrams of Each Other

Page 1 of 1

## 4 Replies - 11493 Views - Last Post: 21 March 2013 - 02:44 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=315715&amp;s=d939003021fcf30b355fabbea6d73d23&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 reetpook

Reputation: 0
• Posts: 2
• Joined: 10-February 13

# Check if Two Strings are Anagrams of Each Other

Posted 17 March 2013 - 12:46 PM

hey

could someone help me writing an algorithm for this. i dont even know where to begin.

Two (or more) words are anagrams if and only if the letters in one can be rearranged to form the other.
For example, the words canter, nectar, recant and trance are all anagrams; they each consist of the same
letters, just in a different order. For this program, you'll write a program that reads a list of words; then
produce a list of how many anagrams each word has, and the largest list of words that are anagrams of
each other.

this is an example our professor gave in class.
>>> ================================ RESTART ================================
>>>
Name of input file? shortwordlist.txt
There were 5 words with 0 anagrams.
There were 4 words with 1 anagrams.
There were 6 words with 2 anagrams.
There were 4 words with 3 anagrams.
The highest anagram count found was 3
The following words have 3 anagrams:
cater
crate
react
trace
>>>

This post has been edited by macosxnerd101: 17 March 2013 - 03:47 PM
Reason for edit:: Renamed title to be more descriptive

Is This A Good Question/Topic? 0

## Replies To: Check if Two Strings are Anagrams of Each Other

### #2 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12241
• Posts: 45,328
• Joined: 27-December 08

## Re: Check if Two Strings are Anagrams of Each Other

Posted 17 March 2013 - 03:46 PM

Big hint- sort the letters. If the two ordered lists are equal, then the words are anagrams.

### #3 Nekroze

Reputation: 14
• Posts: 170
• Joined: 08-May 11

## Re: Check if Two Strings are Anagrams of Each Other

Posted 20 March 2013 - 10:39 PM

Something like this perhaps:

```input1 = "words"
input2 = "sdrow"

assert sorted(list(input1), key=str.lower) == sorted(list(input2), key=str.lower)

```

Simply turns the strings into a list that can be sorted and then uses string.lower to ignore case then uses the natural alphabetical order to sort them, then just looks if they are the same. should work fine.

### #4 Mekire

Reputation: 118
• Posts: 216
• Joined: 11-January 13

## Re: Check if Two Strings are Anagrams of Each Other

Posted 21 March 2013 - 01:44 AM

There is no need to turn the strings into lists before using sorted. Sorted automatically returns a sorted list if the argument is a string. Also, using assert for this strikes me as a little strange. Assert is used for checking if something is the value you expect; and then raises an Assertionerror exception if the case is otherwise. Not really appropriate in this situation in my opinion.

```def are_anagrams(one,two): return sorted(one.lower())==sorted(two.lower())
```

-Mek

### #5 Nekroze

Reputation: 14
• Posts: 170
• Joined: 08-May 11

## Re: Check if Two Strings are Anagrams of Each Other

Posted 21 March 2013 - 02:44 AM

I wasn't 100% sure on the sorted on a string but i figured i would be safe rather then wrong.

I know its may be an annoying habit but when i do code snippets for forums i tend to write them like tests to easily show it works. My bad, just my weird style.

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }