{"id":25597,"date":"2017-10-15T19:53:17","date_gmt":"2017-10-15T14:23:17","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=25597"},"modified":"2017-10-15T19:53:17","modified_gmt":"2017-10-15T14:23:17","slug":"python-programming-write-program-print-permutations-given-string","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/python-programming-write-program-print-permutations-given-string\/","title":{"rendered":"PYTHON Programming for 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.<\/p>\n<p>Below are the permutations of string ABC ABC 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 wp-image-25614 size-full\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/NewPermutation-1.png\" alt=\"python\" 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[pastacode lang=\u201dpython\u201d manual=\u201d%23%20Python%20program%20to%20print%20all%20permutations%20with%0A%23%20duplicates%20allowed%0A%20%0Adef%20toString(List)%3A%0A%20%20%20%20return%20\u201d.join(List)%0A%20%0A%23%20Function%20to%20print%20permutations%20of%20string%0A%23%20This%20function%20takes%20three%20parameters%3A%0A%23%201.%20String%0A%23%202.%20Starting%20index%20of%20the%20string%0A%23%203.%20Ending%20index%20of%20the%20string.%0Adef%20permute(a%2C%20l%2C%20r)%3A%0A%20%20%20%20if%20l%3D%3Dr%3A%0A%20%20%20%20%20%20%20%20print%20toString(a)%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20for%20i%20in%20xrange(l%2Cr%2B1)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20a%5Bl%5D%2C%20a%5Bi%5D%20%3D%20a%5Bi%5D%2C%20a%5Bl%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20permute(a%2C%20l%2B1%2C%20r)%0A%20%20%20%20%20%20%20%20%20%20%20%20a%5Bl%5D%2C%20a%5Bi%5D%20%3D%20a%5Bi%5D%2C%20a%5Bl%5D%20%23%20backtrack%0A%20%0A%23%20Driver%20program%20to%20test%20the%20above%20function%0Astring%20%3D%20%22ABC%22%0An%20%3D%20len(string)%0Aa%20%3D%20list(string)%0Apermute(a%2C%200%2C%20n-1)\u201d message=\u201dPython\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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<p>\u00a0<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PYTHON Programming for Write a program to print all permutations &#8211; Mathematical Algorithms &#8211; A permutation, also called an \u201carrangement number\u201d or \u201corder&#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,74058,83517],"tags":[74203,74205,74201,74195,73683,73681,74192,74181,74200,73689,74186,73676,74199,74207,73714,70379,73700,74191,73693,73699,73692,73707,74206,73696,73717,74194,74198,74184,74188,74209,74189,73680,73697],"class_list":["post-25597","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-coding","category-mathematical-algorithms","category-python-programming","tag-all-combinations","tag-combination-in-python","tag-combination-permutation","tag-example-of-permutation","tag-itertools","tag-itertools-python","tag-itertools-python-3","tag-itertools-python-tutorial","tag-izip-python","tag-next-permutation","tag-permutatio","tag-permutation","tag-permutation-algorithm","tag-permutation-examples","tag-permutation-generator","tag-permutation-in-c","tag-permutation-in-python","tag-permutation-of","tag-permutation-of-string","tag-permutation-of-string-in-java","tag-permutations-of-a-string","tag-permutations-python","tag-print-all-permutation-of-string-both-iterative-and-recursive-way","tag-print-all-permutations-of-a-string","tag-python-product","tag-python-product-of-list","tag-python-random-list","tag-python-repeat","tag-python-shuffle","tag-random-function-in-python","tag-random-int-python","tag-string-permutation","tag-string-permutation-java"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25597","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=25597"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25597\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=25597"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=25597"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=25597"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}