<gouthampradhan>string_TextJustification_1.java
public List<String> fullJustify(String[] words, int L) {
    int wCount = 0, charCount = 0;
    List<String> line = new ArrayList<>();
    List<String> result = new ArrayList<>();
    StringBuilder sb = new StringBuilder();
    for (int i = 0, l = words.length; i < l; i++) {
      String next = words[i];
      if ((L - (charCount + wCount)) >= next.length()) {
        line.add(next);
        charCount += next.length();
        wCount++;
      } else {
        sb.append(line.get(0));
        StringBuilder space = new StringBuilder();
        if (line.size() > 1) {
          int spaceCount = (L - charCount) / (wCount - 1);
          int remaining = (L - charCount) % (wCount - 1);
          for (int j = 0; j < spaceCount; j++) space.append(' ');
          for (int k = 1, kl = line.size(); k < kl; k++) {
            sb.append(space);
            if (remaining > 0) {
              sb.append(' ');
              --remaining;
            }
            sb.append(line.get(k));
          }
        } else {
          int balance = L - (charCount + (wCount - 1));
          for (int j = 0; j < balance; j++) sb.append(' ');
        }
        result.add(sb.toString());
        sb = new StringBuilder();
        line.clear();
        line.add(next);
        charCount = next.length();
        wCount = 1;
      }
    }
    if (!line.isEmpty()) {
      sb.append(line.get(0));
      for (int i = 1, l = line.size(); i < l; i++) {
        sb.append(' ');
        sb.append(line.get(i));
      }
    }
    int balance = L - (charCount + (wCount - 1));
    for (int i = 0; i < balance; i++) sb.append(' ');
    result.add(sb.toString());
    return result;
  }
public List < String > var_1 ( String [ ] var_2 , int var_3 ) {
  int var_5 = 0 , var_6 = 0 ;
  List < String > var_9 = new ArrayList < > ( ) ;
  List < String > var_10 = new ArrayList < > ( ) ;
  StringBuilder var_12 = new StringBuilder ( ) ;
  for ( int var_13 = 0 , var_15 = var_2 . length ;
  var_13 < var_15 ;
  var_13 ++ ) {
    String var_16 = var_2 [ var_13 ] ;
    if ( ( var_3 - ( var_6 + var_5 ) ) >= var_16 . length ( ) ) {
      var_9 . add ( var_16 ) ;
      var_6 += var_16 . length ( ) ;
      var_5 ++ ;
    }
    else {
      var_12 . append ( var_9 . get ( 0 ) ) ;
      StringBuilder var_20 = new StringBuilder ( ) ;
      if ( var_9 . size ( ) > 1 ) {
        int var_23 = ( var_3 - var_6 ) / ( var_5 - 1 ) ;
        int var_24 = ( var_3 - var_6 ) % ( var_5 - 1 ) ;
        for ( int var_25 = 0 ;
        var_25 < var_23 ;
        var_25 ++ ) var_20 . append ( ' ' ) ;
        for ( int var_27 = 1 , var_28 = var_9 . size ( ) ;
        var_27 < var_28 ;
        var_27 ++ ) {
          var_12 . append ( var_20 ) ;
          if ( var_24 > 0 ) {
            var_12 . append ( ' ' ) ;
            -- var_24 ;
          }
          var_12 . append ( var_9 . get ( var_27 ) ) ;
        }
      }
      else {
        int var_29 = var_3 - ( var_6 + ( var_5 - 1 ) ) ;
        for ( int var_25 = 0 ;
        var_25 < var_29 ;
        var_25 ++ ) var_12 . append ( ' ' ) ;
      }
      var_10 . add ( var_12 . toString ( ) ) ;
      var_12 = new StringBuilder ( ) ;
      var_9 . clear ( ) ;
      var_9 . add ( var_16 ) ;
      var_6 = var_16 . length ( ) ;
      var_5 = 1 ;
    }
  }
  if ( ! var_9 . isEmpty ( ) ) {
    var_12 . append ( var_9 . get ( 0 ) ) ;
    for ( int var_13 = 1 , var_15 = var_9 . size ( ) ;
    var_13 < var_15 ;
    var_13 ++ ) {
      var_12 . append ( ' ' ) ;
      var_12 . append ( var_9 . get ( var_13 ) ) ;
    }
  }
  int var_29 = var_3 - ( var_6 + ( var_5 - 1 ) ) ;
  for ( int var_13 = 0 ;
  var_13 < var_29 ;
  var_13 ++ ) var_12 . append ( ' ' ) ;
  var_10 . add ( var_12 . toString ( ) ) ;
  return var_10 ;
}
