Encryption – Hackerrank

Problem Statement

Refer to: https://www.hackerrank.com/challenges/encryption/problem

Solution – Java Program

    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 = col;
        String res = "";
        for(int i=0;i<col;i++)
        {
            for(int j=i;j<len;j = j+col)
            {
                res += s.charAt(j);
            }
            res += " ";
        }
        return res;
    }

Leave a Comment

Your email address will not be published. Required fields are marked *