# Grid Challenge in Algorithm | HackerRank Programming Solutions | HackerRank Problem Solving Solutions in Java [💯Correct]

Hello Programmers/Coders, Today we are going to share solutions of Programming problems of HackerRank, Algorithm Solutions of Problem Solving Section in Java. At Each Problem with Successful submission with all Test Cases Passed, you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.

In this post, you will find the solution for Grid Challenge in Java-HackerRank Problem. We are providing the correct and tested solutions of coding problems present on HackerRank. If you are not able to solve any problem, then you can take help from our Blog/website.

`Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.`

#### Introduction To Algorithm

The word Algorithm means “a process or set of rules to be followed in calculations or other problem-solving operations”. Therefore Algorithm refers to a set of rules/instructions that step-by-step define how a work is to be executed upon in order to get the expected results.

• It is easy to understand.
• Algorithm is a step-wise representation of a solution to a given problem.
• In Algorithm the problem is broken down into smaller pieces or steps hence, it is easier for the programmer to convert it into an actual program.

Link for the ProblemGrid Challenge – Hacker Rank Solution

`Grid Challenge – Hacker Rank Solution`

### Problem:

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 – Hacker Rank Solution`
```import java.io.*;
import java.util.*;

public class Solution {

public static void main(String[] args) throws IOException {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
while(t-->0){
String[]grid = new String[n];
for(int i=0;i<n;i++)

boolean ok = true;

for(int i=0;i<n;i++){
for(int j=1;j<n;j++){
if(grid[j].charAt(i) < grid[j-1].charAt(i)){
ok = false;
break;
}
}
}
System.out.println(ok?"YES":"NO");

}
}

public static String sort(String s){
char[] array = s.toCharArray();
Arrays.sort(array);
return new String(array);
}
}``` 