title: Recursive Word Wrap Algorithm
body: <p>I am working on an algorithm that has three parts. The first is a recursive method that will wrap words to a specific length with the least penalty. The second is an algorithm that is a Dynamic implementation of the recursive method. The last one is a Greedy Algorithm of the problem. I already have the Greedy one coded but I'm struggling on the Recursive solution. I'm not quite sure where exactly I'm running into an issue with my Recursive method but I know it should be something similar to the Knuth-Plass Algorithm. The recursive algorithm is supposed to have a factorial running time, and used more to help with the dynamic solution. If anyone has a link to a Knuth-Plass implementation or can spot something huge in my code, any help would be appreciated.</p>
  
  <p>Recursive Algorithm:</p>
  
  <pre><code>public static ArrayList&lt;String&gt; recursive(ArrayList&lt;String&gt; input, int size) {
      if(input.size() &lt;= 1)
          return input;
      ArrayList&lt;String&gt; temp1 = input;
      ArrayList&lt;String&gt; temp2 = input;
      for(int i = 0; i &lt; input.size(); i++) {
          if(input.size() - 1 &gt;= size)
              break;
          else {
              for(int j = 0; j &lt; input.size(); j++) {
                  temp1.set(j, temp1.get(j) + " " + temp1.get(j + 1));
                  temp1.remove(j + 1);
                  if(totalPenalty(blankChars(temp1, size)) &lt; totalPenalty(blankChars(temp2, size))) {
                      input = recursive(temp1, size);
                  } else {
                      input = recursive(temp2, size);
                  }
              }
          }
      }
      return input;
  }
  </code></pre>
  
  <p>The totalPenalty() and blankChars return the amount of penalty at the end of each line.</p>
  
  <p>EDIT: I'm still not seeing any immediate solutions. Any help would be appreciated.</p>