title: Generating all permutations of a certain length
body: <p>Suppose we have an alphabet "abcdefghiklimnop". How can I recursively generate permutations with repetition of this alphabet in groups of FIVE in an efficient way?</p>
  
  <p>I have been struggling with this a few days now. Any feedback would be helpful.</p>
  
  <p>Essentially this is the same as: <a href="https://stackoverflow.com/questions/4240080/generating-all-permutations-of-a-given-string">Generating all permutations of a given string</a></p>
  
  <p>However, I just want the permutations in lengths of FIVE of the entire string. And I have not been able to figure this out.</p>
  
  <p>SO for all substrings of length 5 of "abcdefghiklimnop", find the permutations of the substring. For example, if the substring was abcdef, I would want all of the permutations of that, or if the substring was defli, I would want all of the permutations of that substring. The code below gives me all permutations of a string but I would like to use to find all permutations of all substrings of size 5 of a string.</p>
  
  <pre><code>    public static void permutation(String str) { 
      permutation("", str); 
  }
  private static void permutation(String prefix, String str) {
      int n = str.length();
      if (n == 0) System.out.println(prefix);
      else {
          for (int i = 0; i &lt; n; i++)
              permutation(prefix + str.charAt(i), str.substring(0, i) + str.substring(i+1, n));
      }
  }
  </code></pre>