Skip to content

Instantly share code, notes, and snippets.

@hxwang
Created January 9, 2016 00:58
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner in = new Scanner(System.in);
int k = in.nextInt();
for(int i=0; i<k; i++){
int n = in.nextInt();
char[][] arr = new char[n][n];
for(int j=0; j<n; j++){
String str = in.next();
arr[j] = str.toCharArray();
}
checkSort(arr);
}
}
public static void checkSort(char[][] arr){
//sort each row
for(int i=0; i<arr.length; i++){
Arrays.sort(arr[i]);
}
//check whether for each column, numbers are in increasing order
for(int j=0; j<arr[0].length; j++){
for(int i=0; i<arr.length-1; i++){
if(arr[i][j]>arr[i+1][j]){
System.out.println("NO");
return;
}
}
}
System.out.println("YES");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment