Grid Challenge HackerRank Solution

Grid Challenge HackerRank Solution

In this Grid Challenge HackerRank solution, Given a square grid of characters in the range ascii[a-z], rearrange elements of each row alphabetically, ascending. Determine if the columns are also in ascending alphabetical order, top to bottom. Return YES if they are or NO if they are not.

Example

The grid is illustrated below.

a b c
a d e
e f g

The rows are already in alphabetical order. The columns a a e, b d f and c e g are also in alphabetical order, so the answer would be YES. Only elements within the same row can be rearranged. They cannot be moved to a different row.

Function Description

Complete the gridChallenge function in the editor below.

gridChallenge has the following parameter(s):

  • string grid[n]: an array of strings

Returns

  • string: either YES or NO

Input Format

The first line contains , the number of testcases.

Each of the next  sets of lines are described as follows:
– The first line contains , the number of rows and columns in the grid.
– The next  lines contains a string of length 

Constraints



Each string consists of lowercase letters in the range ascii[a-z]

Output Format

For each test case, on a separate line print YES if it is possible to rearrange the grid alphabetically ascending in both its rows and columns, or NO otherwise.

Sample Input

STDIN   Function
-----   --------
1       t = 1
5       n = 5
ebacd   grid = ['ebacd', 'fghij', 'olmkn', 'trpqs', 'xywuv']
fghij
olmkn
trpqs
xywuv

Sample Output

YES

Explanation

The x grid in the  test case can be reordered to

abcde
fghij
klmno
pqrst
uvwxy

This fulfills the condition since the rows 1, 2, …, 5 and the columns 1, 2, …, 5 are all alphabetically sorted.

Grid Challenge HackerRank Solution

I will Provide solution in Multiple programming languages for you. If you are not able to find the code in required language then please share in comments so that our team can help you.

Problem Solution in C#

var rows = grid.ConvertAll(row => row.ToArray());
rows.ForEach(Array.Sort);
foreach (var seq in Enumerable.Range(0, rows[0].Length).Select(i => rows.Aggregate("", (agg, row) => agg + row[i])))
{
    for (var i = 1; i < seq.Length; i++)
    {
        if (seq[i] < seq[i - 1])
        {
            return "NO";
        }
    }
}
return "YES";

Problem Solution in Python 3

def gridChallenge(grid):
    n=len(grid)
    ele_n = len(grid[0])
    for i in range(n):
        grid[i] = ''.join(sorted(grid[i]))


    for i in range(n-1):
        for j in range(ele_n):
            if grid[i+1][j] < grid[i][j]:
                return "NO"
    return "YES"

Problem Solution in C++

string gridChallenge(vector<string> grid) {
    for(auto& str : grid)        
        sort(str.begin(), str.end());        
       
    for(size_t i = 0; i < grid.size(); i++)    
        for(size_t j = 1; j < grid.size(); j++)
            if( grid[j][i] < grid[j - 1][i])
                return "NO";    
   
    return "YES";
}

Problem Solution in JavaScript

function gridChallenge(grid) {
    var newArray = [];
    for(var i in grid){
        //convert each string to array
        var temp = Array.from(grid[i]);
        //sort the array
        temp.sort();
        //create list of sorted arrays
        newArray.push(temp);
    }
    var okay = true;
    //check each column
    for(var i=0;i<grid.length;i++){
        for(var j=0;j<grid.length-1;j++){
            if(newArray[j][i]>newArray[j+1][i]){
                okay = false;
                break;
            }
        }
    }
    if(okay==true){
        return 'YES';
    }else{
        return 'NO';
    }
}

Solve original Problem on HackerRank here. Checkout more HackerRank Problems