• + 0 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));

        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();
    
    
    }
    
    • }