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.
My Java solution with o(r * c log c) time complexity and o(r) space:
publicstaticStringgridChallenge(List<String>grid){if(grid.size()==1)return"YES";//no cols to check// 1. sort rows alphabetically ascendingList<String>sortedGrid=newArrayList<>();for(Stringrow:grid){char[]chars=row.toCharArray();Arrays.sort(chars);sortedGrid.add(newString(chars));}// 2. check cols for alphabetically ascending order from top to bottomfor(intcol=0;col<grid.get(0).length();col++){for(introw=1;row<grid.size();row++){StringcurrRow=sortedGrid.get(row);StringprevRow=sortedGrid.get(row-1);//check if col is in orderif(currRow.charAt(col)<prevRow.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 →
My Java solution with o(r * c log c) time complexity and o(r) space: