{"id":5,"date":"2009-11-24T18:07:37","date_gmt":"2009-11-24T18:07:37","guid":{"rendered":"https:\/\/simpleprogrammer.com\/?p=5"},"modified":"2018-02-23T10:50:22","modified_gmt":"2018-02-23T15:50:22","slug":"importunate-permutations","status":"publish","type":"post","link":"https:\/\/simpleprogrammer.com\/importunate-permutations\/","title":{"rendered":"Importunate Permutations"},"content":{"rendered":"<p>Here is an interesting programming problem:\u00a0Calculate all the permutations of a string.<\/p>\n<p>For example, the permutations of &#8220;abc&#8221; are:<\/p>\n<p>abc<\/p>\n<p>acb<\/p>\n<p>bac<\/p>\n<p>bca<\/p>\n<p>cab<\/p>\n<p>cba<\/p>\n<p>It is not as easy of a problem as it seems, but it has a rather simple solution.<\/p>\n<p>Many times recursion is actually more complex to understand than a non-recursive solution, but in this case recursion is an elegant and simple solution.<\/p>\n<p>A good way to solve these kinds of problems is to start with N, N+1, N+2, then solve for N+x.<\/p>\n<p>Let&#8217;s start with N; we will define a function permutation which will give us the permutations of any string.<\/p>\n<p>permutation(&#8220;a&#8221;) =<\/p>\n<p>&#8220;a&#8221;<\/p>\n<p>permutation(&#8220;ab&#8221;) =<\/p>\n<p>&#8220;a&#8221; + permutation(&#8220;b&#8221;)<\/p>\n<p>&#8220;b&#8221; + permutation(&#8220;a&#8221;)<\/p>\n<p>permutation(&#8220;abc&#8221;) =<\/p>\n<p>&#8220;a&#8221; + permutation(&#8220;bc&#8221;)<\/p>\n<p>&#8220;b&#8221; + permutation(&#8220;ac&#8221;)<\/p>\n<p>&#8220;c&#8221; + permutation(&#8220;ab&#8221;)<\/p>\n<p>permutation(x)<\/p>\n<p>foreach character c in x<\/p>\n<p>c + permutation(x remove c)<\/p>\n<p>Looking at it in C# code, it would look something like:<\/p>\n<p><script src=\"https:\/\/gist.github.com\/simpleprogrammer-shared\/77efc41de2611f0b1013f85736c7092e.js\"><\/script><\/p>\n<p>Update: Here is a pretty slick example in LINQ that I got from a stackoverflow.com <a href=\"http:\/\/stackoverflow.com\/questions\/774457\/combination-generator-in-linq\" target=\"_blank\">question<\/a>.<\/p>\n<p><script src=\"https:\/\/gist.github.com\/simpleprogrammer-shared\/40746350cb16810dd68578d0d04bf0aa.js\"><\/script><\/p>\n<p>For a better understanding of permutations, check out this short booklet on <a href=\"http:\/\/www.amazon.com\/gp\/product\/B008KMEMXQ\/ref=as_li_ss_tl?ie=UTF8&amp;camp=1789&amp;creative=390957&amp;creativeASIN=B008KMEMXQ&amp;linkCode=as2&amp;tag=makithecompsi-20\" target=\"_blank\">How to Understand Permutations and Computations<\/a> .<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Here is an interesting programming problem:\u00a0Calculate all the permutations of a string. For example, the permutations of &#8220;abc&#8221; are: abc acb bac bca cab cba It is not as easy of a problem as it seems, but it has a rather simple solution. Many times recursion is actually more complex to understand than a non-recursive&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","footnotes":""},"categories":[3378,2426,2813],"tags":[],"class_list":["post-5","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-c","category-math"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Importunate Permutations - Simple Programmer<\/title>\n<meta name=\"description\" content=\"Examples of how to calculate all the permutations of a string in a simple and elegant manner. Sample code is provided in C# with a solution using LINQ.\" \/>\n<meta name=\"robots\" content=\"noindex, follow\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Importunate Permutations - Simple Programmer\" \/>\n<meta property=\"og:description\" content=\"Examples of how to calculate all the permutations of a string in a simple and elegant manner. Sample code is provided in C# with a solution using LINQ.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/simpleprogrammer.com\/importunate-permutations\/\" \/>\n<meta property=\"og:site_name\" content=\"Simple Programmer\" \/>\n<meta property=\"article:published_time\" content=\"2009-11-24T18:07:37+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-02-23T15:50:22+00:00\" \/>\n<meta name=\"author\" content=\"John Sonmez\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"John Sonmez\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/importunate-permutations\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/importunate-permutations\\\/\"},\"author\":{\"name\":\"John Sonmez\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#\\\/schema\\\/person\\\/1abc70edfb189184827740310672de67\"},\"headline\":\"Importunate Permutations\",\"datePublished\":\"2009-11-24T18:07:37+00:00\",\"dateModified\":\"2018-02-23T15:50:22+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/importunate-permutations\\\/\"},\"wordCount\":191,\"commentCount\":0,\"articleSection\":[\"Algorithms\",\"C#\",\"Math\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/simpleprogrammer.com\\\/importunate-permutations\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/importunate-permutations\\\/\",\"url\":\"https:\\\/\\\/simpleprogrammer.com\\\/importunate-permutations\\\/\",\"name\":\"Importunate Permutations - Simple Programmer\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#website\"},\"datePublished\":\"2009-11-24T18:07:37+00:00\",\"dateModified\":\"2018-02-23T15:50:22+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#\\\/schema\\\/person\\\/1abc70edfb189184827740310672de67\"},\"description\":\"Examples of how to calculate all the permutations of a string in a simple and elegant manner. Sample code is provided in C# with a solution using LINQ.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/importunate-permutations\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/simpleprogrammer.com\\\/importunate-permutations\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/importunate-permutations\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/simpleprogrammer.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Importunate Permutations\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#website\",\"url\":\"https:\\\/\\\/simpleprogrammer.com\\\/\",\"name\":\"Simple Programmer\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/simpleprogrammer.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#\\\/schema\\\/person\\\/1abc70edfb189184827740310672de67\",\"name\":\"John Sonmez\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g\",\"caption\":\"John Sonmez\"},\"url\":\"https:\\\/\\\/simpleprogrammer.com\\\/author\\\/jsonmez\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Importunate Permutations - Simple Programmer","description":"Examples of how to calculate all the permutations of a string in a simple and elegant manner. Sample code is provided in C# with a solution using LINQ.","robots":{"index":"noindex","follow":"follow"},"og_locale":"en_US","og_type":"article","og_title":"Importunate Permutations - Simple Programmer","og_description":"Examples of how to calculate all the permutations of a string in a simple and elegant manner. Sample code is provided in C# with a solution using LINQ.","og_url":"https:\/\/simpleprogrammer.com\/importunate-permutations\/","og_site_name":"Simple Programmer","article_published_time":"2009-11-24T18:07:37+00:00","article_modified_time":"2018-02-23T15:50:22+00:00","author":"John Sonmez","twitter_card":"summary_large_image","twitter_misc":{"Written by":"John Sonmez","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/simpleprogrammer.com\/importunate-permutations\/#article","isPartOf":{"@id":"https:\/\/simpleprogrammer.com\/importunate-permutations\/"},"author":{"name":"John Sonmez","@id":"https:\/\/simpleprogrammer.com\/#\/schema\/person\/1abc70edfb189184827740310672de67"},"headline":"Importunate Permutations","datePublished":"2009-11-24T18:07:37+00:00","dateModified":"2018-02-23T15:50:22+00:00","mainEntityOfPage":{"@id":"https:\/\/simpleprogrammer.com\/importunate-permutations\/"},"wordCount":191,"commentCount":0,"articleSection":["Algorithms","C#","Math"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/simpleprogrammer.com\/importunate-permutations\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/simpleprogrammer.com\/importunate-permutations\/","url":"https:\/\/simpleprogrammer.com\/importunate-permutations\/","name":"Importunate Permutations - Simple Programmer","isPartOf":{"@id":"https:\/\/simpleprogrammer.com\/#website"},"datePublished":"2009-11-24T18:07:37+00:00","dateModified":"2018-02-23T15:50:22+00:00","author":{"@id":"https:\/\/simpleprogrammer.com\/#\/schema\/person\/1abc70edfb189184827740310672de67"},"description":"Examples of how to calculate all the permutations of a string in a simple and elegant manner. Sample code is provided in C# with a solution using LINQ.","breadcrumb":{"@id":"https:\/\/simpleprogrammer.com\/importunate-permutations\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/simpleprogrammer.com\/importunate-permutations\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/simpleprogrammer.com\/importunate-permutations\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/simpleprogrammer.com\/"},{"@type":"ListItem","position":2,"name":"Importunate Permutations"}]},{"@type":"WebSite","@id":"https:\/\/simpleprogrammer.com\/#website","url":"https:\/\/simpleprogrammer.com\/","name":"Simple Programmer","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/simpleprogrammer.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/simpleprogrammer.com\/#\/schema\/person\/1abc70edfb189184827740310672de67","name":"John Sonmez","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g","caption":"John Sonmez"},"url":"https:\/\/simpleprogrammer.com\/author\/jsonmez\/"}]}},"_links":{"self":[{"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/posts\/5","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/comments?post=5"}],"version-history":[{"count":0,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/posts\/5\/revisions"}],"wp:attachment":[{"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/media?parent=5"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/categories?post=5"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/tags?post=5"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}