We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I am not sure if my solution is the most elegant, even thoug less time consume:
/* Time complexity: O(n): length og the grid O(nlogn): Sort each row O(n^2): Check each column = O(n + nlogn + n^2) = O(n^2 logn) */publicstaticStringgridChallenge(List<String>grid){// Sort the rowsfor(inti=0;i<grid.size();i++){char[]chars=grid.get(i).toCharArray();Arrays.sort(chars);grid.set(i,newString(chars));}// Check if each column is sorted alphabeticallyfor(intcol=0;col<grid.get(0).length();col++){// for each row, it checks if the current (row, col) is greater than next (row,col)for(introw=0;row<grid.size()-1;row++){if(grid.get(row).charAt(col)>grid.get(row+1).charAt(col)){return"NO";}}}return"YES";}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Grid Challenge
You are viewing a single comment's thread. Return to all comments →
I am not sure if my solution is the most elegant, even thoug less time consume: