-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathEncryption.java
31 lines (25 loc) · 1019 Bytes
/
Encryption.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// https://www.hackerrank.com/challenges/encryption/problem
package implimentation;
import java.util.Scanner;
public class Encryption {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String string = scanner.next();
System.out.println(encrypted(string));
}
private static String encrypted(String string) {
String message = String.join("", string.split(" "));
double key = Math.sqrt(message.length());
int rows = (int) key;
int columns = (int) Math.ceil(key);
rows += rows * columns < message.length() ? 1 : 0 ;
StringBuilder encrypted = new StringBuilder();
for (int column = 0; column < columns ; column++) {
for (int row = 0 ; row < rows && row * columns + column < message.length() ; row++) {
encrypted.append(message.charAt(row * columns + column));
}
encrypted.append(" ");
}
return encrypted.toString();
}
}