Skip to content

Instantly share code, notes, and snippets.

@samarthsewlani
Created March 13, 2023 08:53
Show Gist options
  • Save samarthsewlani/30cb7dc4d29f712236a4becd2d7658e0 to your computer and use it in GitHub Desktop.
Save samarthsewlani/30cb7dc4d29f712236a4becd2d7658e0 to your computer and use it in GitHub Desktop.
Grid Challenge HackerRank Solution
public static String gridChallenge(List<String> grid) {
List<String> sortedStrings = new ArrayList();
int n = grid.size();
int m = grid.get(0).length();
for(int i=0;i<grid.size();i++){
String s = grid.get(i);
char charArr[] = s.toCharArray();
Arrays.sort(charArr);
sortedStrings.add(new String(charArr));
System.out.println(new String(charArr));
}
//Check columnwise sorting
for(int i=1;i<n;i++){
for(int j=0;j<m;j++){
if( sortedStrings.get(i-1).charAt(j) > sortedStrings.get(i).charAt(j) ) return "NO";
}
}
return "YES";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment