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
Calculating the minimum square is quite easy. you can just check the no of characters in input string and the minimum square size n*n.
1*1= 1
2*2 = 4
3*3 = 9
so you can easily find the n by putting in formula
n*n >= length(input string).
As I understand:
You have
ABCHIDGFE
You convert it to square (if possible)
A B C
H I D
G F E
And then make string going clockwise
A B C D E F G H I
And return this string
I'm not sure what to do if it's impossible to make square of it.
Try this
package com.misc;
public class SprintSpiral {
public static void main(String[] args){
int xStart = 0;
int xEnd = 3;
int yStart = 0;
int yEnd = 3;
int[][] arr = new int[4][4];
arr[0][0]=1;
arr[1][0]=2;
arr[2][0]=3;
arr[3][0]=4;
arr[0][1]=5;
arr[1][1]=6;
arr[2][1]=7;
arr[3][1]=8;
arr[0][2]=9;
arr[1][2]=10;
arr[2][2]=11;
arr[3][2]=14;
arr[0][3]=15;
arr[1][3]=16;
arr[2][3]=17;
arr[3][3]=18;
for (int i = 0; i < 16; i++) {
for (int j = xStart; j <= xEnd; j++) {
System.out.println(arr[j][yStart]);
}
++yStart;
for (int j = yStart; j <= yEnd; j++) {
System.out.println(arr[xEnd][j]);
}
xEnd--;
for (int j = xEnd; j >= xStart; j--) {
System.out.println(arr[j][yEnd]);
}
yEnd--;
for (int j = yEnd; j >= yStart; j--) {
System.out.println(arr[xStart][j]);
}
xStart++;
}
}
}
//in c++
string convert(const string & s) { int len = s.size();
//base case
if (len == 0) return string("");
// minimum square calculation
int i = 1;
while (i*i < len) ++i;
//cout << "i=" << i << endl;
//matrix initialization
vector<vector<char> > matrix;
matrix.resize(i);
for (int j = 0; j < i; ++j)
matrix[j].resize(i);
for (int j = 0; j < i; ++j)
for (int k = 0; k < i; ++k)
matrix[j][k] = ' ';
//logic
int r = 0, c = 0;
bool right = true, down = false, left = false, up = false;
int curr_len = 0;
int side = i - 1;
while (curr_len < len)
{
if (right)
{
for (int j = 1; (j <= side) && ((curr_len+j) <= len); ++j)
{
matrix[r][c] = s[curr_len];
//cout << curr_len << "|" << r << "|" << c << "|" << matrix[r][c] << "\n";
++c;
++curr_len;
}
right = false;
down = true;
}
if (down)
{
for (int j = 1; (j <= side) && ((curr_len+j) <= len); ++j)
{
matrix[r][c] = s[curr_len];
//cout << curr_len << "|" << r << "|" << c << "|" << matrix[r][c] << "\n";
++r;
++curr_len;
}
down = false;
left = true;
}
if (left)
{
for (int j = 1; (j <= side) && ((curr_len+j) <= len); ++j)
{
matrix[r][c] = s[curr_len];
//cout << curr_len << "|" << r << "|" << c << "|" << matrix[r][c] << "\n";
--c;
++curr_len;
}
left = false;
up = true;
}
if (up)
{
for (int j = 1; (j <= side) && ((curr_len+j) <= len); ++j)
{
matrix[r][c] = s[curr_len];
//cout << curr_len << "|" << r << "|" << c << "|" << matrix[r][c] << "\n";
--r;
++curr_len;
}
up = false;
right = true;
side = side - 2;
++r; ++c;
}
}
stringstream ss;
for (int j = 0; j < i; ++j)
{
for (int k = 0; k < i; ++k)
{
ss << matrix[j][k];
}
}
return ss.str();
}
This is what I understand of the question.
Lets say we have the string "hello world this was the first script I ever programmed how are you today good"
This string has 64 characters.
Now if you look at the string you gave, it has 36 characters, of which the square root is 6, hence 6 letters on each side.
So the string above has 64 characters of which the square root is: 8
which means the minimum square would need to be 8 letters on each side.
This answer deals with the process behind the 'calculate the minimum size square' requirement.
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<numcols && position < input.length;i++) {
output[row][col+i] = input[position++];
}
if (position < input.length){
placedown(output, input, position, row+1, col+numcols-1, numrows-1, numcols);
}
}
public static void placedown(char output[][], char input[], int position, int row, int col, int numrows, int numcols) {
for (int i=0;i<numrows && position < input.length;i++) {
output[row+i][col] = input[position++];
}
if (position < input.length){
placeleft(output, input, position, row+numrows-1, col-1, numrows, numcols-1);
}
}
public static void placeleft(char output[][], char input[], int position, int row, int col, int numrows, int numcols) {
for (int i=0;i<numcols && position < input.length;i++) {
output[row][col-i] = input[position++];
}
if (position < input.length){
placeup(output, input, position, row-1, col-numcols+1, numrows-1, numcols);
}
}
public static void placeup(char output[][], char input[], int position, int row, int col, int numrows, int numcols) {
for (int i=0;i<numrows && position < input.length;i++) {
output[row-i][col] = input[position++];
}
if (position < input.length){
placeright(output, input, position, row-numrows+1, col+1, numrows, numcols-1);
}
}
public static void main( String[] args )
{
String input = "paypalisthefastersaferwaytosendmoney".toUpperCase();
char chars[] = input.toCharArray();
int sqrtceil = (int) Math.ceil(Math.sqrt(chars.length));
int rows = sqrtceil;
int cols = sqrtceil;
while (cols*(rows-1) >= chars.length) {
rows--;
}
char output[][] = new char[rows][cols];
placeright(output, chars, 0, 0, 0, rows, cols);
for (int i=0;i<output.length;i++) {
for (int j=0;j<output[i].length;j++) {
System.out.print(output[i][j] + " ");
}
System.out.println();
}
}