{"id":729,"date":"2017-02-21T11:41:58","date_gmt":"2017-02-21T19:41:58","guid":{"rendered":"http:\/\/www.tech.dimprash.com\/?p=729"},"modified":"2017-02-21T12:19:20","modified_gmt":"2017-02-21T20:19:20","slug":"given-a-sequence-of-words-print-all-anagrams-together","status":"publish","type":"post","link":"http:\/\/www.tech.dimprash.com\/?p=729","title":{"rendered":"Given a sequence of words, print all anagrams together"},"content":{"rendered":"<pre>\r\n$sortFunc = function($val) {\r\n   $strArr = str_split($val);\r\n   sort($strArr);\r\n   return implode(\"\", $strArr);\r\n};\r\n\r\n$arr = array(\"cat\", \"dog\", \"tac\", \"god\", \"act\");\r\n\r\n$words = array();\r\n$index = array();\r\n\r\n\/\/ form a new array with each string sorted within itself\r\n$words = array_map($sortFunc, $arr);\r\n\/\/ form a index array in order to remember original index\r\nforeach ($words as $key => $val) {\r\n    $index[$key] = $val;\r\n}\r\nasort($index);  \/\/ sort the values but maintain index association\r\nforeach ($index as $key => $val) {\r\n    \/\/ use index value in the original array to print\r\n    echo $arr[$key] . \"\\n\";\r\n}\r\n\r\n<\/pre>\n<p>http:\/\/www.geeksforgeeks.org\/given-a-sequence-of-words-print-all-anagrams-together\/<\/p>\n","protected":false},"excerpt":{"rendered":"<p>$sortFunc = function($val) { $strArr = str_split($val); sort($strArr); return implode(&#8220;&#8221;, $strArr); }; $arr = array(&#8220;cat&#8221;, &#8220;dog&#8221;, &#8220;tac&#8221;, &#8220;god&#8221;, &#8220;act&#8221;); $words = array(); $index = array(); \/\/ form a new array with each string sorted within itself $words = array_map($sortFunc, $arr); \/\/ form a index array in order to remember original index foreach ($words as $key &hellip; <a href=\"http:\/\/www.tech.dimprash.com\/?p=729\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Given a sequence of words, print all anagrams together<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-729","post","type-post","status-publish","format-standard","hentry","category-algorithms"],"_links":{"self":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/729","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=729"}],"version-history":[{"count":3,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/729\/revisions"}],"predecessor-version":[{"id":732,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/729\/revisions\/732"}],"wp:attachment":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=729"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=729"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=729"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}