For simplicity , 1 extra space is used in all below arrays extras [ i ] [ j ] will have number of extra spaces if words from i to j are put in a single line
int extras [ ] [ ] = new int [ n + 1 ] [ n + 1 ] ;