-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGridChallenge.java
57 lines (42 loc) · 1.07 KB
/
GridChallenge.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import java.util.Arrays;
import java.util.Scanner;
public class GridChallenge {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0)
{
int n=sc.nextInt();
char arr[][]=new char[n][n];
int row=0;
while(row<n){
String s=sc.next();
for(int i=0;i<n;i++)
{
arr[row][i]=s.charAt(i);
}
row++;
}
if(isValid(arr))
System.out.println("YES");
else
System.out.println("NO");
}
}
private static boolean isValid(char[][] arr) {
boolean f=true;
for(char a[]:arr)
Arrays.sort(a);
for(int i=0;i<arr.length-1;i++)
{
for(int j=0;j<arr[0].length;j++){
if(arr[i][j]>arr[i+1][j])
{
f=false;
break;
}
}
}
return f;
}
}