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 Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s