Search This Blog

Saturday, November 23, 2013

Hacker Rank Problem - Algorithm - Search- Encryption

Encryption 
The following is the solution to Hacker Rank problem Encryption using Java.  For solutions to other Hacker Rank Problem visit my page HackerRank, alternatively try searching for the problem in my blog.

Score:25/25
/**
 *
 */
package hackerRank;

import java.util.Scanner;

/**
 * @author Arun.G
 *
 */
public class Encryption {

       /**
        * @param args
        */
       public static void main(String[] args) {
              // TODO Auto-generated method stub

              Scanner sc = new Scanner(System.in);
              String word = sc.nextLine();
             
              // Remove all white space characters
              word = word.replaceAll("\\s+", "");
             
              //compute the width and height
              int width = (int) Math.ceil(Math.sqrt(word.length()));
              int height = (int) Math.floor(Math.sqrt(word.length()));
             
              int newHeight = 0, newWidth = 0;
              //some times we can fit in the the word with given width find and increase the height
              if (word.length() / height >= width)
                     height += 1;

              String result = "";

              char[][] secret = null;
              //find the correction width and height or not equal
              int correction = Math.abs(width - height);
               // if height is greater, correct the width and make it equal to height
              if (height > width)
              {
                     secret = new char[height][width + correction];
                     newWidth = width + correction;
                     newHeight = height;
              }
              //if width is greater, correct the height and make it equal to width
              else if (width > height)
              {

                     secret = new char[height + correction][width];
                     newHeight = height + correction;
                     newWidth = width;
              }
              //else height and width are equal
              else
              {
                     secret = new char[height][width];
                     newHeight = height;
                     newWidth = width;

              } 
              int index = 0;
              //write down the message in the row major order in array
              for (int i = 0; i < height; i++) {
                     for (int j = 0; j < width; j++) {
                           if (index + 1 <= word.length()) {
                                  secret[i][j] = word.charAt(index);
                                  index++;
                           }
                     }
              }
              //get the message from the array in column major order
              for (int i = 0; i < newHeight; i++) {
                     for (int j = 0; j < newWidth; j++) {
                           //if the character is not null add it to result
                           if ((secret[j][i] != '\0'))
                                  result += Character.toString(secret[j][i]);
                     }
                     result += " ";
              }

              System.out.println(result);
              sc.close();
       }

}

No comments:

Post a Comment

Labels