{"id":25595,"date":"2017-10-15T19:31:14","date_gmt":"2017-10-15T14:01:14","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=25595"},"modified":"2017-10-15T19:31:14","modified_gmt":"2017-10-15T14:01:14","slug":"c-programming-write-program-print-permutations-given-string-2-2","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-programming-write-program-print-permutations-given-string-2-2\/","title":{"rendered":"C++ 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(<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=\"c++\" 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=\u201dcpp\u201d manual=\u201d%2F%2F%20C%20program%20to%20print%20all%20permutations%20with%20duplicates%20allowed%0A%23include%20%3Cstdio.h%3E%0A%23include%20%3Cstring.h%3E%0A%20%0A%2F*%20Function%20to%20swap%20values%20at%20two%20pointers%20*%2F%0Avoid%20swap(char%20*x%2C%20char%20*y)%0A%7B%0A%20%20%20%20char%20temp%3B%0A%20%20%20%20temp%20%3D%20*x%3B%0A%20%20%20%20*x%20%3D%20*y%3B%0A%20%20%20%20*y%20%3D%20temp%3B%0A%7D%0A%20%0A%2F*%20Function%20to%20print%20permutations%20of%20string%0A%20%20%20This%20function%20takes%20three%20parameters%3A%0A%20%20%201.%20String%0A%20%20%202.%20Starting%20index%20of%20the%20string%0A%20%20%203.%20Ending%20index%20of%20the%20string.%20*%2F%0Avoid%20permute(char%20*a%2C%20int%20l%2C%20int%20r)%0A%7B%0A%20%20%20int%20i%3B%0A%20%20%20if%20(l%20%3D%3D%20r)%0A%20%20%20%20%20printf(%22%25s%5Cn%22%2C%20a)%3B%0A%20%20%20else%0A%20%20%20%7B%0A%20%20%20%20%20%20%20for%20(i%20%3D%20l%3B%20i%20%3C%3D%20r%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20swap((a%2Bl)%2C%20(a%2Bi))%3B%0A%20%20%20%20%20%20%20%20%20%20permute(a%2C%20l%2B1%2C%20r)%3B%0A%20%20%20%20%20%20%20%20%20%20swap((a%2Bl)%2C%20(a%2Bi))%3B%20%2F%2Fbacktrack%0A%20%20%20%20%20%20%20%7D%0A%20%20%20%7D%0A%7D%0A%20%0A%2F*%20Driver%20program%20to%20test%20above%20functions%20*%2F%0Aint%20main()%0A%7B%0A%20%20%20%20char%20str%5B%5D%20%3D%20%22ABC%22%3B%0A%20%20%20%20int%20n%20%3D%20strlen(str)%3B%0A%20%20%20%20permute(str%2C%200%2C%20n-1)%3B%0A%20%20%20%20return%200%3B%0A%7D\u201d message=\u201dC++\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<p>\u00a0<\/p>\n","protected":false},"excerpt":{"rendered":"<p>C++ Programming for Write a program to print all permutations of a given string &#8211; Mathematical Algorithms -A permutation also called an \u201carrangement number\u201d<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,83515,1,74058],"tags":[74260,74268,70339,74265,74277,74274,70349,70372,70375,74270,70323,70348,70331,70326,70340,70338,74276,70366,74267,70328,74261,73693,70384,73690,74275,74262,70329,70347,74271,73711,74263,74259,73395,73148],"class_list":["post-25595","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-c-programming-3","category-coding","category-mathematical-algorithms","tag-combination-math","tag-combination-of-numbers","tag-combinations-and-permutations","tag-combinatorics","tag-examples-of-permutation-and-combination","tag-examples-of-permutations-and-combinations","tag-formula-for-combinations","tag-formula-for-permutation-and-combination","tag-formula-of-permutation-and-combination","tag-online-permutation-calculator","tag-permutation-and-combination","tag-permutation-and-combination-calculator","tag-permutation-and-combination-formula","tag-permutation-and-combination-pdf","tag-permutation-and-combination-problems","tag-permutation-combination","tag-permutation-combination-examples","tag-permutation-combination-formula","tag-permutation-examples-math","tag-permutation-formula","tag-permutation-formula-calculator","tag-permutation-of-string","tag-permutation-program-in-c","tag-permutation-questions","tag-permutation-with-repetition","tag-permutation-word-problems","tag-permutations-and-combinations","tag-permutations-and-combinations-formula","tag-permutations-and-combinations-pdf","tag-printall","tag-software-engineer-interview-questions-and-answers","tag-solving-for-r-in-permutations","tag-string-in-c","tag-string-programs-in-c"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25595","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=25595"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25595\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=25595"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=25595"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=25595"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}