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.
publicstaticStringencryption(Strings){// Remove spaces from the strings=s.replaceAll("\\s","");intlength=s.length();// Determine grid dimensionsintfloor=(int)Math.floor(Math.sqrt(length));intceil=(int)Math.ceil(Math.sqrt(length));// Adjust rows and columns based on calculated dimensionsintrows=floor;intcolumns=ceil;if(rows*columns<length){if(ceil*ceil>=length){rows=ceil;columns=ceil;}elseif(ceil*floor>=length){rows=ceil;}}StringBuilderencrypted=newStringBuilder();// Construct column-wise encryptionfor(intcol=0;col<columns;col++){for(introw=0;row<rows;row++){intindex=row*columns+col;if(index<length){encrypted.append(s.charAt(index));}}if(col<columns-1){encrypted.append(' ');}}returnencrypted.toString();}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Encryption
You are viewing a single comment's thread. Return to all comments →