You are viewing a single comment's thread. Return to all comments →
public static String encryption(String s) { // Write your code here int size = s.length();
int x = (int)Math.floor(Math.sqrt(size)); int y = (int)Math.ceil(Math.sqrt(size)); if(x * y < size){ x = y; } char[][] t = new char[x][y]; int index = 0; for(int i = 0; i < x; i++ ){ for(int j = 0; j < y; j++){ if(index == size){ break; } t[i][j] = s.charAt(index); index++; } } int io = 0; StringBuilder enc = new StringBuilder(); for(int i = 0; i < y; i++){ if( i != 0){ enc.append(" "); } for(int j = 0; j < x; j++){ if(t[j][i] == 0){ continue; } enc.append(t[j][i]); io++; if(io == size){ break; } } } return enc.toString(); }
Seems like cookies are disabled on this browser, please enable them to open this website
I agree to HackerRank's Terms of Service and Privacy Policy.
Encryption
You are viewing a single comment's thread. Return to all comments →
public static String encryption(String s) { // Write your code here int size = s.length();