# HackerRank : NCR CodeSprint – Spiral Message

By | November 6, 2016

You’ve intercepted an encoded spy message! The message originated as a single line of one or more space-separated words, but it was encoded into an matrix as a clockwise spiral starting in the lower left-hand corner. For example, the diagram below shows the decoding process for an encoded message:

The message is decoded spirally starting from the lower left-hand corner of the matrix and moving in the clockwise direction (i.e., up, right, down, left, up, right, etc.). From the starting position, you must clockwise-traverse the matrix, scanning characters and switching to the next clockwise direction each time you reach a boundary (i.e., an already-scanned character or the end of the matrix). Continue scanning characters in this manner until you’ve scanned all the matrix’s characters into a single decoded string. The word separator for the decoded string is the hash mark (#).

Given , , and an encoded message, decode the message and print the number of words in the decoded message.

Input Format

The first line contains two space-separated positive integers describing the respective values of and .
Each line of the subsequent lines contains a string of characters describing row of the encoded message.

Constraints

• Each word consists of lowercase English alphabetic characters (a to z).
• The encoded message consists of words and hash marks (#). Each hash mark denotes a single space.

Output Format

Print an integer denoting the number of decoded words.

Sample Input

3 5
a##ar
a#aa#
xxwsr


Sample Output

4


Explanation

The diagram at the top of the challenge demonstrates the decoding process for the given Sample Input. The decoded message is xaa##ar#rswx#aa. Because hash marks denote spaces, we can break the message into four words: xaa, ar, rswx, and aa. Thus, we print as our answer.

Solution

I have solved the problem, but few test cases are not passing.
Checking the code again.

Till now:
I have created a function which creates a linear string from the matrix traversing
it in spiral order.
This function returns the string to the main function.
In main function, I am putting logic to remove duplicate consecutive '#', and finally
I am counting the number of '#' in the string, answer is count + 1.

Code:

package NCRCodeSprint;

import java.util.Scanner;

public class SpiralMessage2 {

static String spiralPrint(int m, int n, char a[][])
{
StringBuilder sB= new StringBuilder();

int i, k = 0, l = 0;

/*  k - starting row index
m - ending row index
l - starting column index
n - ending column index
i - iterator
*/

while (k < m && l < n)
{
/* traversing the first row from the remaining rows */
for (i = l; i < n; ++i)
sB.append(a[k][i]);
k++;

/* traversing the last column from the remaining columns */
for (i = k; i < m; ++i)
sB.append(a[i][n-1]);
n--;

/* traversing the last row from the remaining rows */
if ( k = l; --i)
sB.append(a[m-1][i]);
m--;
}

/* traversing the first column from the remaining columns */
if (l = k; --i)
sB.append(a[i][l]);
l++;
}
}
return sB.toString();

}

public static void main(String[] args) {

Scanner scan= new Scanner(System.in);
int n=scan.nextInt();
int m=scan.nextInt();
scan.nextLine();

char a[][]= new char[n][m];

for(int i=0;i<n;i++)
{
String line=scan.nextLine();
for(int j=0;j<m;j++)
{
a[i][j]=line.charAt(j);
}
}

StringBuilder sB2=new StringBuilder(spiralPrint(n, m, a));

for(int k=0;k<sB2.length()-1;k++)
{
if(sB2.charAt(k)=='#' && sB2.charAt(k+1)=='#')
sB2.replace(k,k+1, "&");

if(sB2.charAt(k)=='&' && sB2.charAt(k+1)=='#')
sB2.replace(k,k+1, "&");

}

//System.out.println(sB2);

int count=1;
for(int k=0;k<sB2.length()-1;k++)
{
if(sB2.charAt(k)=='#')
count++;
}

if(n==1 && m==1 && a[0][0]=='#')
System.out.println(0);
else
System.out.println(count);

}
}
/*
3 5
a##ar
a#aa#
xxwsr
*/