Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are right that recursion is the way to go. The example you worked thru w/ the little bit of math was all correct, but kind of indirect.</p> <p>Here's some pseudocode:</p> <pre><code> def permute(charSet, soFar): if charSet is empty: print soFar //base case for each element 'e' of charSet: permute(charSet without e, soFar + e) //recurse </code></pre> <p>example of partial recursion tree</p> <pre><code> permute({A,B,C}, '') / | \ permute({B,C}, 'A') permute({A,C}, 'B') permute({A,B}, 'C') / \ permute({A}, 'BC') permute({C}, 'BA') | permute({}, 'BCA')&lt;---BASE CASE, print 'BCA' </code></pre> <hr> <h2>EDIT</h2> <p>To handle repeated characters without duplicating any permutations. Let <code>unique</code> be a function to remove any repeated elements from a collection (or string that is being treated like an ordered character collection thru indexing).</p> <p>1) Store results (including dupes), filter them out afterwards</p> <pre><code> def permuteRec(charSet, soFar): if charSet is empty: tempResults.add(soFar) //base case for each element 'e' of charSet: permute(charSet without e, soFar + e) //recurse global tempResults[] def permute(inString): permuteRec(inString, '') return unique(tempResults) print permute(inString) </code></pre> <p>2) Avoid generating duplicates in the first place</p> <pre><code> def permute(charSet, soFar): if charSet is empty: print soFar //base case for each element 'e' of unique(charSet): permute(charSet without e, soFar + e) //recurse </code></pre>
    singulars
    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