A very clean single function to solve HackerRank's contest that has been explained in V. Anton Spraul vedio.
A word from the English dictionary is taken and arranged as a matrix. e.g. "MATHEMATICS"
There are many ways to trace this matrix in a way that helps you construct this word. You start tracing the matrix from the top-left position and at each iteration, you either move RIGHT or DOWN, and ultimately reach the bottom-right of the matrix. It is assured that any such tracing generates the same word. How many such tracings can be possible for a given word of length m+n-1 written as a matrix of size m * n?
int trace(int row, int col)
static int count = 0;
if (row == 1 && col == 1)
if (row <= 0 || col <= 0)
trace(row, col - 1);
trace(row - 1, col);
trace( 4 , 3 );
Recursive upward traversal as shown in the next figure.