Grid Challenge Hackerrank Solution - java
For Explanation Watch Video ::
Code ::
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
class Result {
public static String sort(String s){
char[] ch = s.toCharArray();
Arrays.sort(ch);
return new String(ch);
}
public static String gridChallenge(List<String> grid) {
ArrayList<String> al = new ArrayList<>();
for(int i=0;i<grid.size();i++){
String s = sort(grid.get(i));
al.add(s);
}
int size = al.get(0).length();
for(int i=1;i<al.size();i++){
for(int j=0;j<size;j++){
if(al.get(i-1).charAt(j)>al.get(i).charAt(j)){
return "NO";
}
}
}
return "YES";
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
int t = Integer.parseInt(bufferedReader.readLine().trim());
IntStream.range(0, t).forEach(tItr -> {
try {
int n = Integer.parseInt(bufferedReader.readLine().trim());
List<String> grid = IntStream.range(0, n).mapToObj(i -> {
try {
return bufferedReader.readLine();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
})
.collect(toList());
String result = Result.gridChallenge(grid);
bufferedWriter.write(result);
bufferedWriter.newLine();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
});
bufferedReader.close();
bufferedWriter.close();
}
}
Comments
Post a Comment