<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: algorithm to group all anagrams from a dictionary</title>
	<atom:link href="http://blog.kapilchhabra.com/2007/05/algorithm-to-group-all-anagrams-from-a-dictionary-2/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.kapilchhabra.com/2007/05/algorithm-to-group-all-anagrams-from-a-dictionary-2/</link>
	<description>my experiments with IT and beyond</description>
	<lastBuildDate>Sat, 17 Jul 2010 12:56:59 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: satnam</title>
		<link>http://blog.kapilchhabra.com/2007/05/algorithm-to-group-all-anagrams-from-a-dictionary-2/comment-page-1/#comment-84</link>
		<dc:creator>satnam</dc:creator>
		<pubDate>Wed, 04 Jul 2007 09:37:00 +0000</pubDate>
		<guid isPermaLink="false">http://blog.kapilchhabra.com/?p=25#comment-84</guid>
		<description>Or we can do it like this...&lt;br/&gt;&lt;br/&gt;Assign each alphabet a unique number&lt;br/&gt;a-1&lt;br/&gt;b-2&lt;br/&gt;c-3&lt;br/&gt;.&lt;br/&gt;.&lt;br/&gt;now for each word do addition something like this.. &lt;br/&gt;&lt;br/&gt;for e.g. if word is &quot;cba&quot; its unique sum is &lt;br/&gt;3*(26)*(26)*(26) + 2*(26)*(26) + 1*(26)= watever&lt;br/&gt;&lt;br/&gt;so by doing this,cab,abc,bac all would give this same sum and map to same hash position.&lt;br/&gt;&lt;br/&gt;Of course this would reduce the time because we are not sorting but just adding. &lt;br/&gt;So time reduces from nlogn to n.&lt;br/&gt;&lt;br/&gt;Problem with this solution is we have to check the length of the string.&lt;br/&gt;&lt;br/&gt;To take care of that ,here is sunny&#039;s better solution.&lt;br/&gt;&lt;br/&gt;assign each alphabet a unique prime number. Now instead of adding we multiply the corresponding prime values of each aplphabet in a word to get a unique number. This would take care of size too...&lt;br/&gt;&lt;br/&gt;Cheers :)&lt;br/&gt;&lt;br/&gt;(as there are 26 alphabets) and check and store this word&#039;s position in some hash</description>
		<content:encoded><![CDATA[<p>Or we can do it like this&#8230;</p>
<p>Assign each alphabet a unique number<br />a-1<br />b-2<br />c-3<br />.<br />.<br />now for each word do addition something like this.. </p>
<p>for e.g. if word is &#8220;cba&#8221; its unique sum is <br />3*(26)*(26)*(26) + 2*(26)*(26) + 1*(26)= watever</p>
<p>so by doing this,cab,abc,bac all would give this same sum and map to same hash position.</p>
<p>Of course this would reduce the time because we are not sorting but just adding. <br />So time reduces from nlogn to n.</p>
<p>Problem with this solution is we have to check the length of the string.</p>
<p>To take care of that ,here is sunny&#8217;s better solution.</p>
<p>assign each alphabet a unique prime number. Now instead of adding we multiply the corresponding prime values of each aplphabet in a word to get a unique number. This would take care of size too&#8230;</p>
<p>Cheers <img src='http://blog.kapilchhabra.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>(as there are 26 alphabets) and check and store this word&#8217;s position in some hash</p>
]]></content:encoded>
	</item>
</channel>
</rss>
