Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Assuming:</p> <p>1) You don't care too much that this is O(n*2^n)... although I'm curious to know: what is the best asymptotic run time for this type of problem?</p> <p>2) Your input is all lowercase.</p> <p>3) Your input is &lt; 32 characters long. (# of usable bits in the permutation counter, i)</p> <pre><code> List&lt;string&gt; permutate(string word) { List&lt;string&gt; ret = new List&lt;string&gt;(); // MAGIC HAPPENS HERE Dictionary&lt;char,char&gt; toUppers = new Dictionary&lt;char,char&gt;(26); toUppers.Add('a', 'A'); toUppers.Add('b', 'B'); toUppers.Add('c', 'C'); toUppers.Add('d', 'D'); toUppers.Add('e', 'E'); toUppers.Add('f', 'F'); toUppers.Add('g', 'G'); toUppers.Add('h', 'H'); toUppers.Add('i', 'I'); toUppers.Add('j', 'J'); toUppers.Add('k', 'K'); toUppers.Add('l', 'L'); toUppers.Add('m', 'M'); toUppers.Add('n', 'N'); toUppers.Add('o', 'O'); toUppers.Add('p', 'P'); toUppers.Add('q', 'Q'); toUppers.Add('r', 'R'); toUppers.Add('s', 'S'); toUppers.Add('t', 'T'); toUppers.Add('u', 'U'); toUppers.Add('v', 'V'); toUppers.Add('w', 'W'); toUppers.Add('x', 'X'); toUppers.Add('y', 'Y'); toUppers.Add('z', 'Z'); char[] wordChars = word.ToCharArray(); int len = wordChars.Length; // iterate the number of permutations for(int i = 0; i &lt; 2^len; i++) { char[] newWord = new char[len](); // apply "i" as a bitmask to each original char for(int n = 0; n &lt; newWord.Length; n++) { if((1 &lt;&lt; n) &amp; i != 0) { newWord[n] = toUppers[wordChars[n]]; // or skip the dictionary and just call Char.ToUpper(wordChars[n]) } else { newWord[n] = wordChars[n]; } } ret.Add(new String(newWord)); } return ret; } </code></pre> <p>Note: I haven't compiled or tested this code. This is also implementing the bitwise compare that sharptooth suggested above.</p>
 

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