-
Notifications
You must be signed in to change notification settings - Fork 2
/
Grid_Challenge.java
33 lines (30 loc) · 995 Bytes
/
Grid_Challenge.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
import java.io.*;
import java.util.*;
import java.util.Arrays;
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
while(T-- > 0){
int N = sc.nextInt();
boolean flag = false;
char [][] A = new char [N][N];
if(N == 0) {System.out.println("YES"); continue;}
for(int i = 0; i < N; ++i){
String str = sc.next();
for(int j = 0; j < N; ++j){
A[i][j] = str.charAt(j);
}
Arrays.sort(A[i]);
}
for(int j = 0; j < N ; ++j){
for(int i = 0; i < N - 1; ++i){
if(A[i][j] > A[i + 1][j]){System.out.println("NO"); flag = true; break;}
}
if(flag) break;
}
if(!flag) System.out.println("YES");
}
}
}