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 += " "; } … Continue reading Encryption – Hackerrank

Huffman Decoding – Hackerrank

Problem Statement Refer: https://www.hackerrank.com/challenges/tree-huffman-decoding/problem Solution void decode(String s, Node root) { StringBuilder sb = new StringBuilder(); Node c = root; for(int i=0;i<s.length();i++) { c = s.charAt(i) == '1' ? c.right : c.left; if(c.left == null && c.right == null) { sb.append(c.data); c = root; } } System.out.println(sb); }