Created
March 13, 2023 08:53
-
-
Save samarthsewlani/30cb7dc4d29f712236a4becd2d7658e0 to your computer and use it in GitHub Desktop.
Grid Challenge HackerRank Solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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