{"id":770,"date":"2017-03-24T13:04:01","date_gmt":"2017-03-24T21:04:01","guid":{"rendered":"http:\/\/www.tech.dimprash.com\/?p=770"},"modified":"2017-03-24T13:04:16","modified_gmt":"2017-03-24T21:04:16","slug":"find-all-permutations-of-a-given-string","status":"publish","type":"post","link":"http:\/\/www.tech.dimprash.com\/?p=770","title":{"rendered":"Find all permutations of a given string"},"content":{"rendered":"<p><strong> Find all permutations of a given string:<\/strong> <\/p>\n<pre>\r\n\/\/ function to generate and print all N! permutations of $str. (N = strlen($str)).\r\nfunction permute($str,$i,$n) {\r\n   if ($i == $n)\r\n       print \"$str\\n\";\r\n   else {\r\n        for ($j = $i; $j < $n; $j++) {\r\n          swap($str,$i,$j);\r\n          permute($str, $i+1, $n);\r\n          swap($str,$i,$j); \/\/ backtrack.\r\n       }\r\n   }\r\n}\r\n\r\n\/\/ function to swap the char at pos $i and $j of $str.\r\nfunction swap(&#038;$str,$i,$j) {\r\n    $temp = $str[$i];\r\n    $str[$i] = $str[$j];\r\n    $str[$j] = $temp;\r\n}   \r\n\r\n$str = \"hey\";\r\npermute($str,0,strlen($str)); \/\/ call the function.\r\n<\/pre>\n<p>Diagram:  http:\/\/www.geeksforgeeks.org\/write-a-c-program-to-print-all-permutations-of-a-given-string\/<\/p>\n<p>PHP Implementation:<br \/>\n<a href=\"http:\/\/stackoverflow.com\/questions\/2617055\/how-to-generate-all-permutations-of-a-string-in-php\">http:\/\/stackoverflow.com\/questions\/2617055\/how-to-generate-all-permutations-of-a-string-in-php<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Find all permutations of a given string: \/\/ function to generate and print all N! permutations of $str. (N = strlen($str)). function permute($str,$i,$n) { if ($i == $n) print &#8220;$str\\n&#8221;; else { for ($j = $i; $j < $n; $j++) { swap($str,$i,$j); permute($str, $i+1, $n); swap($str,$i,$j); \/\/ backtrack. } } } \/\/ function to swap &hellip; <a href=\"http:\/\/www.tech.dimprash.com\/?p=770\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Find all permutations of a given string<\/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-770","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\/770","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=770"}],"version-history":[{"count":2,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/770\/revisions"}],"predecessor-version":[{"id":772,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/770\/revisions\/772"}],"wp:attachment":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=770"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=770"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=770"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}