Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is one in C#, which should work even with repeated characters. For example on "banana" for permutations of length 2 it gives:</p> <blockquote> <p>ba bn ab aa an nb na nn</p> </blockquote> <p>The basic idea is to fix the first character, then form all permutations of length k-1, then prepend the character to those k-1 length permutations. To deal with duplicate characters, we keep track of the count left (i.e the ones which can be used for sub-permutations).</p> <p>Not exemplary code, but should give you the idea. (If you find bugs, let me know and I can edit).</p> <pre><code>static List&lt;string&gt; Permutations(Dictionary&lt;char, int&gt; input, int length) { List&lt;string&gt; permutations = new List&lt;string&gt;(); List&lt;char&gt; chars = new List&lt;char&gt;(input.Keys); // Base case. if (length == 0) { permutations.Add(string.Empty); return permutations; } foreach (char c in chars) { // There are instances of this character left to use. if (input[c] &gt; 0) { // Use one instance up. input[c]--; // Find sub-permutations of length length -1. List&lt;string&gt; subpermutations = Permutations(input, length - 1); // Give back the instance. input[c]++; foreach (string s in subpermutations) { // Prepend the character to be the first character. permutations.Add(s.Insert(0,new string(c,1))); } } } return permutations; } </code></pre> <p>And here is the full program I have, to use it:</p> <pre><code>using System; using System.Collections.Generic; namespace StackOverflow { class Program { static void Main(string[] args) { List&lt;string&gt; p = Permutations("abracadabra", 3); foreach (string s in p) { Console.WriteLine(s); } } static List&lt;string&gt; Permutations(string s, int length) { Dictionary&lt;char, int&gt; input = new Dictionary&lt;char, int&gt;(); foreach (char c in s) { if (input.ContainsKey(c)) { input[c]++; } else { input[c] = 1; } } return Permutations(input, length); } static List&lt;string&gt; Permutations(Dictionary&lt;char, int&gt; input, int length) { List&lt;string&gt; permutations = new List&lt;string&gt;(); List&lt;char&gt; chars = new List&lt;char&gt;(input.Keys); if (length == 0) { permutations.Add(string.Empty); return permutations; } foreach (char c in chars) { if (input[c] &gt; 0) { input[c]--; List&lt;string&gt; subpermutations = Permutations(input, length - 1); input[c]++; foreach (string s in subpermutations) { permutations.Add(s.Insert(0,new string(c,1))); } } } return permutations; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload