MCPcopy Create free account
hub / github.com/MohamedMetwalli5/HackerRank-Solutions / Encryption

Class Encryption

ProblemSolving (General)/Encryption.java:9–59  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

7import java.util.regex.*;
8
9public class Encryption {
10
11 // Complete the encryption function below.
12 static String encryption(String s) {
13 String result = s.replaceAll("\\s","");
14 int r = (int)Math.floor(Math.sqrt(s.length()));
15 int c = (int)Math.ceil(Math.sqrt(s.length()));
16 if(r*c < s.length()){
17 r++;
18 }
19 int k = 0;
20 char[][] array = new char[r][c];
21 for(int i=0;i<r;i++){
22 for(int j=0;j<c;j++){
23 if(k <= s.length()-1){
24 array[i][j] = result.charAt(k);
25 k++;
26 }
27 }
28 }
29 StringBuilder str = new StringBuilder();
30 for(int i=0;i<c;i++){
31 for(int j=0;j<r;j++){
32 if(array[j][i] != '\u0000'){
33 str.append(array[j][i]);
34 }
35 }
36 str.append(' ');
37
38 }
39 return str.toString().trim();
40
41 }
42
43 private static final Scanner scanner = new Scanner(System.in);
44
45 public static void main(String[] args) throws IOException {
46 BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
47
48 String s = scanner.nextLine();
49
50 String result = encryption(s);
51
52 bufferedWriter.write(result);
53 bufferedWriter.newLine();
54
55 bufferedWriter.close();
56
57 scanner.close();
58 }
59}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected