I had recently participated in coding competion sponsored by an company and there was this one question which I did not understood, as to what was it asking.
Here i
People are using lots of nested loops and if statements in their solutions above. Personally, i find it cleaner to think of how to do this in terms of:
direction
current input position
current row
current column
num rows to fill
num cols to fill
Fill right row 0 from column 0 to column 5.
Fill down column 5 from row 1 to row 4.
Fill left row 5 from column 4 to column 0
Fill up column 0 from row 4 to row 1
etc...
It's a classic recursive solution that could even be modified for a fork-join pool if you really wanted to. This particular solution actually adjusts the output grid size according to the input, so it's possible you might get a 5 rows x 6 cols if you trim enough chars off the input (though you could always leave the row trimming out and just produce a square with lots of blanks).
public static void placeright(char output[][], char input[], int position, int row, int col, int numrows, int numcols) {
for (int i=0;i= chars.length) {
rows--;
}
char output[][] = new char[rows][cols];
placeright(output, chars, 0, 0, 0, rows, cols);
for (int i=0;i