-
Notifications
You must be signed in to change notification settings - Fork 3
/
Encryption.java
48 lines (38 loc) · 1.16 KB
/
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.hackerrank;
import java.util.Scanner;
public class Encryption {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s = in.nextLine();
System.out.println(encrypt(s));
}
static String encrypt(String line) {
String s = line.replaceAll(" ", "");
double len = s.length();
int row = (int) Math.floor(Math.sqrt(len));
int col = (int) Math.ceil(Math.sqrt(len));
if (row * col < s.length()) {
row = col;
}
char[][] arr = new char[row][col];
int k = 0;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (k < s.length()) {
arr[i][j] = s.charAt(k);
k++;
}
}
}
StringBuilder result = new StringBuilder();
for (int j = 0; j < col; j++) {
for (int i = 0; i < row; i++) {
if (arr[i][j] != '\0') {
result.append(arr[i][j]);
}
}
result.append(" ");
}
return result.toString().trim();
}
}