{"id":25517,"date":"2017-10-15T19:05:42","date_gmt":"2017-10-15T13:35:42","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=25517"},"modified":"2017-10-15T19:05:42","modified_gmt":"2017-10-15T13:35:42","slug":"write-program-print-permutations-given-string-4","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/write-program-print-permutations-given-string-4\/","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\" target=\"_blank\" rel=\"noopener\">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-25516\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/NewPermutation.png\" alt=\"Write a program to print all permutations of a given string\" width=\"1288\" height=\"524\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/NewPermutation.png 1288w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/NewPermutation-300x122.png 300w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/NewPermutation-768x312.png 768w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/NewPermutation-1024x417.png 1024w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/NewPermutation-990x403.png 990w\" sizes=\"(max-width: 1288px) 100vw, 1288px\" \/><\/p>\n<p><strong>JAVA Programming<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Java<\/span> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">\/\/ Java program to print all permutations of a<br\/>\/\/ given string.<br\/>public class Permutation<br\/>{<br\/>    public static void main(String[] args)<br\/>    {<br\/>        String str = &quot;ABC&quot;;<br\/>        int n = str.length();<br\/>        Permutation permutation = new Permutation();<br\/>        permutation.permute(str, 0, n-1);<br\/>    }<br\/> <br\/>    \/**<br\/>     * permutation function<br\/>     * @param str string to calculate permutation for<br\/>     * @param l starting index<br\/>     * @param r end index<br\/>     *\/<br\/>    private void permute(String str, int l, int r)<br\/>    {<br\/>        if (l == r)<br\/>            System.out.println(str);<br\/>        else<br\/>        {<br\/>            for (int i = l; i &lt;= r; i++)<br\/>            {<br\/>                str = swap(str,l,i);<br\/>                permute(str, l+1, r);<br\/>                str = swap(str,l,i);<br\/>            }<br\/>        }<br\/>    }<br\/> <br\/>    \/**<br\/>     * Swap Characters at position<br\/>     * @param a string value<br\/>     * @param i position 1<br\/>     * @param j position 2<br\/>     * @return swapped string<br\/>     *\/<br\/>    public String swap(String a, int i, int j)<br\/>    {<br\/>        char temp;<br\/>        char[] charArray = a.toCharArray();<br\/>        temp = charArray[i] ;<br\/>        charArray[i] = charArray[j];<br\/>        charArray[j] = temp;<br\/>        return String.valueOf(charArray);<br\/>    }<br\/> <br\/>}<\/code><\/pre> <\/div>\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=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>JAVA Programming-Write a program to print all permutations of a given string &#8211; Searching and Sorting &#8211; 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. <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,2139,71670,83549],"tags":[73701,73700,73693,73699,70384,73690,73692,73707,73696,73713,73703,73708,73682,73710,73702,73057,73685,73686,73687,73718,73680,73697,73148],"class_list":["post-25517","post","type-post","status-publish","format-standard","hentry","category-coding","category-java","category-searching-and-sorting","category-strings","tag-permutation-in-java","tag-permutation-in-python","tag-permutation-of-string","tag-permutation-of-string-in-java","tag-permutation-program-in-c","tag-permutation-questions","tag-permutations-of-a-string","tag-permutations-python","tag-print-all-permutations-of-a-string","tag-print-list-python","tag-python-combinations","tag-python-itertools-combinations","tag-python-language-example","tag-python-list-unique","tag-python-permutations","tag-python-print-list","tag-recursion-examples","tag-recursion-in-python","tag-sample-python-script","tag-simple-python-script","tag-string-permutation","tag-string-permutation-java","tag-string-programs-in-c"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25517","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=25517"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25517\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=25517"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=25517"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=25517"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}