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.
This is my solution in Java language. The special thing is that I used Counting sort while reading and sorting the list into an integer matrix. I don't know whether i can change the order of characters of String in Java (because String in Java is commonly a literal).
My code (including estimated time&space complexity):
publicstaticStringgridChallenge(List<String>grid){// Write your code here//Read input and sort - Space: O(n^2)int[][]matrix=newint[grid.size()][grid.get(0).length()];for(inti=0;i<grid.size();i++){//O(n^2)sort(grid.get(i),matrix[i]);//O(n)}// Then check and returnbooleancheck=check(matrix);//O(n^2)return(check)?"YES":"NO";}publicstaticvoidsort(Stringline,int[]arr){//Counting sort//Space: O(1)//Time: O(n) + O(1) + O(n) -> O(n)int[]alpha=newint[26];inttemp;for(inti=0;i<line.length();i++){//O(n)alpha[line.charAt(i)-'a']++;}for(inti=1;i<alpha.length;i++){//O(1)alpha[i]+=alpha[i-1];}for(inti=line.length()-1;i>=0;i--){//O(n)temp=line.charAt(i)-'a';arr[--alpha[temp]]=temp;}}publicstaticbooleancheck(int[][]matrix){//Time: O(n^2)for(intj=0;j<matrix[0].length;j++){for(inti=1;i<matrix.length;i++){if(matrix[i][j]<matrix[i-1][j])returnfalse;}}returntrue;}
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 →
This is my solution in Java language. The special thing is that I used Counting sort while reading and sorting the list into an integer matrix. I don't know whether i can change the order of characters of String in Java (because String in Java is commonly a literal). My code (including estimated time&space complexity):