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(n * m) time complexity and o(n) space complexity:
publicstaticStringencryption(Strings){// remove the spaces from the strings.replaceAll(" ","");// get the len of the stringintlength=s.length();// set x equal to floor of sqrt of lenintx=(int)Math.floor(Math.sqrt(length));// set y equal to ceil of sqrt of leninty=(int)Math.ceil(Math.sqrt(length));if(x*y<length)x+=1;// print each char of the string x by yStringBuilderencryptedString=newStringBuilder();for(inti=0;i<y;i++){for(intj=0;j<x;j++){intidx=j*y+i;if(idx<length)encryptedString.append(s.charAt(idx));}encryptedString.append(' ');//line break}returnencryptedString.toString();}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
An unexpected error occurred. Please try reloading the page. If problem persists, please contact support@hackerrank.com
Encryption
You are viewing a single comment's thread. Return to all comments →
My Java solution with o(n * m) time complexity and o(n) space complexity: