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.
public static String encryption(String s) {
int len = s.length();
int row = (int) Math.floor(Math.sqrt(len));
int col = (int) Math.ceil(Math.sqrt(len));
if (row * col < len) {
row++;
}
char[][] matrix = new char[row][col];
int index = 0;
for (int r = 0; r < row; r++) {
for (int c = 0; c < col; c++) {
if (index < len) {
matrix[r][c] = s.charAt(index++);
} else {
matrix[r][c] = ' ';
}
}
}
StringBuilder sb = new StringBuilder();
for (int c = 0; c < col; c++) {
if (c > 0) sb.append(" ");
for (int r = 0; r < row; r++) {
if (matrix[r][c] != ' ') {
sb.append(matrix[r][c]);
}
}
}
return sb.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 →
public static String encryption(String s) { int len = s.length(); int row = (int) Math.floor(Math.sqrt(len)); int col = (int) Math.ceil(Math.sqrt(len));