{"id":25596,"date":"2017-10-15T20:09:38","date_gmt":"2017-10-15T14:39:38","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=25596"},"modified":"2017-10-15T20:09:38","modified_gmt":"2017-10-15T14:39:38","slug":"java-programming-write-program-print-permutations-given-string","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/java-programming-write-program-print-permutations-given-string\/","title":{"rendered":"JAVA Programming- Write a program to print all permutations of a given string"},"content":{"rendered":"<p>A permutation, also called an \u201carrangement number\u201d or \u201corder,\u201d is a rearrangement of the elements of an ordered list S into a one-to-one correspondence with S itself. A string of length n has n! permutation.<span id=\"more-767\"><\/span><br \/>\nSource: Mathword(<a href=\"http:\/\/mathworld.wolfram.com\/Permutation.html\">http:\/\/mathworld.wolfram.com\/Permutation.html<\/a>)<\/p>\n<p>Below are the permutations of string ABC.<br \/>\nABC ACB BAC BCA CBA CAB<\/p>\n<p>Here is a solution that is used as a basis in backtracking.<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-25614\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/NewPermutation-1.png\" alt=\"java\" width=\"1288\" height=\"524\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/NewPermutation-1.png 1288w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/NewPermutation-1-300x122.png 300w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/NewPermutation-1-768x312.png 768w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/NewPermutation-1-1024x417.png 1024w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/NewPermutation-1-990x403.png 990w\" sizes=\"(max-width: 1288px) 100vw, 1288px\" \/><\/p>\n<p>\u00a0<\/p>\n<p>\u00a0<\/p>\n<p>\u00a0<\/p>\n[pastacode lang=\u201djava\u201d manual=\u201d%2F%2F%20Java%20program%20to%20print%20all%20permutations%20of%20a%0A%2F%2F%20given%20string.%0Apublic%20class%20Permutation%0A%7B%0A%20%20%20%20public%20static%20void%20main(String%5B%5D%20args)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20String%20str%20%3D%20%22ABC%22%3B%0A%20%20%20%20%20%20%20%20int%20n%20%3D%20str.length()%3B%0A%20%20%20%20%20%20%20%20Permutation%20permutation%20%3D%20new%20Permutation()%3B%0A%20%20%20%20%20%20%20%20permutation.permute(str%2C%200%2C%20n-1)%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F**%0A%20%20%20%20%20*%20permutation%20function%0A%20%20%20%20%20*%20%40param%20str%20string%20to%20calculate%20permutation%20for%0A%20%20%20%20%20*%20%40param%20l%20starting%20index%0A%20%20%20%20%20*%20%40param%20r%20end%20index%0A%20%20%20%20%20*%2F%0A%20%20%20%20private%20void%20permute(String%20str%2C%20int%20l%2C%20int%20r)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20if%20(l%20%3D%3D%20r)%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.println(str)%3B%0A%20%20%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20(int%20i%20%3D%20l%3B%20i%20%3C%3D%20r%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20str%20%3D%20swap(str%2Cl%2Ci)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20permute(str%2C%20l%2B1%2C%20r)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20str%20%3D%20swap(str%2Cl%2Ci)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F**%0A%20%20%20%20%20*%20Swap%20Characters%20at%20position%0A%20%20%20%20%20*%20%40param%20a%20string%20value%0A%20%20%20%20%20*%20%40param%20i%20position%201%0A%20%20%20%20%20*%20%40param%20j%20position%202%0A%20%20%20%20%20*%20%40return%20swapped%20string%0A%20%20%20%20%20*%2F%0A%20%20%20%20public%20String%20swap(String%20a%2C%20int%20i%2C%20int%20j)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20char%20temp%3B%0A%20%20%20%20%20%20%20%20char%5B%5D%20charArray%20%3D%20a.toCharArray()%3B%0A%20%20%20%20%20%20%20%20temp%20%3D%20charArray%5Bi%5D%20%3B%0A%20%20%20%20%20%20%20%20charArray%5Bi%5D%20%3D%20charArray%5Bj%5D%3B%0A%20%20%20%20%20%20%20%20charArray%5Bj%5D%20%3D%20temp%3B%0A%20%20%20%20%20%20%20%20return%20String.valueOf(charArray)%3B%0A%20%20%20%20%7D%0A%20%0A%7D\u201d message=\u201dJava\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>\u00a0<\/p>\n<p><strong>Output:<\/strong><\/p>\n<pre>ABC\r\nACB\r\nBAC\r\nBCA\r\nCBA\r\nCAB<\/pre>\n<p><strong><br \/>\nAlgorithm Paradigm: <\/strong>Backtracking<br \/>\n<strong>Time Complexity: <\/strong>O(n*n!) Note that there are n! permutations and it requires O(n) time to print a a permutation.<\/p>\n[ad type=\u201dbanner\u201d]\n","protected":false},"excerpt":{"rendered":"<p>JAVA Programming for Write a program to print all permutations of a given string &#8211; Mathematical Algorithms &#8211; A permutation also called \u201carrangement number&#8221;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,1,2139,74058,83549],"tags":[73755,74249,74091,74245,73721,74252,74251,74234,74238,74247,74254,74255,74239,74191,74246,73693,70384,74258,73690,74256,74257,74241,73148,73458,74250,74233,74243,74244],"class_list":["post-25596","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-coding","category-java","category-mathematical-algorithms","category-strings","tag-all-string-programs-in-java","tag-anagram-program-in-java","tag-basic-programming-interview-questions","tag-c-program-for-permutation","tag-c-program-for-permutation-of-numbers","tag-combinations-java","tag-find-all-permutations-of-a-string-java","tag-java-combinations","tag-java-permutation-algorithm","tag-java-program-to-print-all-possible-combinations-of-a-number","tag-java-recursion-example","tag-lexicographic-order-java","tag-permutation-generator-online","tag-permutation-of","tag-permutation-of-numbers-in-java","tag-permutation-of-string","tag-permutation-program-in-c","tag-permutation-program-in-java","tag-permutation-questions","tag-permutation-without-repetition","tag-recursion-java-example","tag-string-algorithm-interview-questions","tag-string-programs-in-c","tag-string-programs-in-java","tag-string-programs-in-java-for-interview","tag-what-is-string-in-java","tag-write-a-program-to","tag-write-a-program-to-print"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25596","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=25596"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25596\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=25596"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=25596"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=25596"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}