{"id":4356,"date":"2022-04-25T04:30:14","date_gmt":"2022-04-25T04:30:14","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=4356"},"modified":"2022-04-26T13:05:42","modified_gmt":"2022-04-26T13:05:42","slug":"find-all-permutations-of-a-string","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2022\/04\/25\/find-all-permutations-of-a-string\/","title":{"rendered":"Find All Permutations of a String"},"content":{"rendered":"\n<p><strong>OVERVIEW<\/strong><\/p>\n\n\n\n<p>The code given below prints all the possible permutations of a string. It does not check for duplicate occurrences of a character.<\/p>\n\n\n\n<p><strong>SAMPLE CODE<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\npublic class StringPermutation {\n\n\tpublic void permute(String str, int start, int stop) {\n\t\n\t\tif (stop == start) {\n\t\t  System.out.println(str);\n\t\t  return;\n\t\t}\n\t\tfor(int i=start;i &lt;= stop; i++) {\n\t\t\tstr = swap(str, start, i);\n\t\t\tpermute(str, start+1, stop);\n\t\t\t\n\t\t}\n\t}\n\n\tpublic String swap(String str, int a, int b) {\n\t  char&#x5B;] chars = str.toCharArray();\n\t  char temp = chars&#x5B;a];\n\t  chars&#x5B;a] = chars&#x5B;b];\n\t  chars&#x5B;b] = temp;\n\t  \n\t  return String.valueOf(chars);\n\t}\n\n\n\tpublic static void main(String args&#x5B;]) {\n\t   StringPermutation perm = new StringPermutation();\n\t   if (args.length &lt; 1) {\n\t      System.out.println(&quot;Needs string argument&quot;);\n\t      return;\n\t   }\n\t   String str = args&#x5B;0];\n\t   System.out.println(&quot;Processing &quot; + str + &quot; of length  &quot; + str.length());\n\t   perm.permute(str, 0, str.length()-1);\n\t}\n}\n\n<\/pre><\/div>\n\n\n<p>The output is given below:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">amit@amit-viao:\/var\/projects\/java$ java StringPermutation abc\n Processing abc of length  3\n abc\n acb\n bac\n bca\n cba\n cab<\/pre>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>OVERVIEW The code given below prints all the possible permutations of a string. It does not check for duplicate occurrences of a character. SAMPLE CODE <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2022\/04\/25\/find-all-permutations-of-a-string\/\" title=\"Find All Permutations of a String\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":4308,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[366,319],"tags":[],"class_list":["post-4356","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-computer-science","category-java"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4356","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=4356"}],"version-history":[{"count":2,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4356\/revisions"}],"predecessor-version":[{"id":4359,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4356\/revisions\/4359"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media\/4308"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=4356"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=4356"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=4356"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}