Hackerrank Encryption Solution
.MathJax_SVG_Display {text-align: center; margin: 1em 0em; position: relative; display: block!important; text-indent: 0; max-width: none; max-height: none; min-width: 0; min-height: 0; width: 100%} .MathJax_SVG .MJX-monospace {font-family: monospace} .MathJax_SVG .MJX-sans-serif {font-family: sans-serif} .MathJax_SVG {display: inline; font-style: normal; font-weight: normal; line-height: normal; font-size: 100%; font-size-adjust: none; text-indent: 0; text-align: left; text-transform: none; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0; min-height: 0; border: 0; padding: 0; margin: 0} .MathJax_SVG * {transition: none; -webkit-transition: none; -moz-transition: none; -ms-transition: none; -o-transition: none} .mjx-svg-href {fill: blue; stroke: blue} .MathJax_SVG_LineBox {display: table!important} .MathJax_SVG_LineBox span {display: table-cell!important; width: 10000em!important; min-width: 0; max-width: none; padding: 0; border: 0; margin: 0}
An English text needs to be encrypted using the following encryption scheme.
First, the spaces are removed from the text. Let be the length of this text.
Then, characters are written into a grid, whose rows and columns have the following constraints:
For example, the sentence , after removing spaces is characters long. is between and , so it is written in the form of a grid with 7 rows and 8 columns.
ifmanwas
meanttos
tayonthe
groundgo
dwouldha
vegivenu
sroots
- Ensure that
- If multiple grids satisfy the above conditions, choose the one with the minimum area, i.e. .
The encoded message is obtained by displaying the characters in a column, inserting a space, and then displaying the next column and inserting a space, and so on. For example, the encoded message for the above rectangle is:
imtgdvs fearwer mayoogo anouuio ntnnlvt wttddes aohghn sseoau
You will be given a message to encode and print.
Function Description
Complete the encryption function in the editor below. It should return a single string composed as described.
encryption has the following parameter(s):
- s: a string to encrypt
Input Format.MathJax_SVG_Display {text-align: center; margin: 1em 0em; position: relative; display: block!important; text-indent: 0; max-width: none; max-height: none; min-width: 0; min-height: 0; width: 100%} .MathJax_SVG .MJX-monospace {font-family: monospace} .MathJax_SVG .MJX-sans-serif {font-family: sans-serif} .MathJax_SVG {display: inline; font-style: normal; font-weight: normal; line-height: normal; font-size: 100%; font-size-adjust: none; text-indent: 0; text-align: left; text-transform: none; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0; min-height: 0; border: 0; padding: 0; margin: 0} .MathJax_SVG * {transition: none; -webkit-transition: none; -moz-transition: none; -ms-transition: none; -o-transition: none} .mjx-svg-href {fill: blue; stroke: blue} .MathJax_SVG_LineBox {display: table!important} .MathJax_SVG_LineBox span {display: table-cell!important; width: 10000em!important; min-width: 0; max-width: none; padding: 0; border: 0; margin: 0}
One line of text, the string
Constraints.MathJax_SVG_Display {text-align: center; margin: 1em 0em; position: relative; display: block!important; text-indent: 0; max-width: none; max-height: none; min-width: 0; min-height: 0; width: 100%} .MathJax_SVG .MJX-monospace {font-family: monospace} .MathJax_SVG .MJX-sans-serif {font-family: sans-serif} .MathJax_SVG {display: inline; font-style: normal; font-weight: normal; line-height: normal; font-size: 100%; font-size-adjust: none; text-indent: 0; text-align: left; text-transform: none; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0; min-height: 0; border: 0; padding: 0; margin: 0} .MathJax_SVG * {transition: none; -webkit-transition: none; -moz-transition: none; -ms-transition: none; -o-transition: none} .mjx-svg-href {fill: blue; stroke: blue} .MathJax_SVG_LineBox {display: table!important} .MathJax_SVG_LineBox span {display: table-cell!important; width: 10000em!important; min-width: 0; max-width: none; padding: 0; border: 0; margin: 0}
is comprised only of characters in the range ascii[a-z].
Output Format.MathJax_SVG_Display {text-align: center; margin: 1em 0em; position: relative; display: block!important; text-indent: 0; max-width: none; max-height: none; min-width: 0; min-height: 0; width: 100%} .MathJax_SVG .MJX-monospace {font-family: monospace} .MathJax_SVG .MJX-sans-serif {font-family: sans-serif} .MathJax_SVG {display: inline; font-style: normal; font-weight: normal; line-height: normal; font-size: 100%; font-size-adjust: none; text-indent: 0; text-align: left; text-transform: none; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0; min-height: 0; border: 0; padding: 0; margin: 0} .MathJax_SVG * {transition: none; -webkit-transition: none; -moz-transition: none; -ms-transition: none; -o-transition: none} .mjx-svg-href {fill: blue; stroke: blue} .MathJax_SVG_LineBox {display: table!important} .MathJax_SVG_LineBox span {display: table-cell!important; width: 10000em!important; min-width: 0; max-width: none; padding: 0; border: 0; margin: 0}
Print the encoded message on one line as described.
Sample Input.MathJax_SVG_Display {text-align: center; margin: 1em 0em; position: relative; display: block!important; text-indent: 0; max-width: none; max-height: none; min-width: 0; min-height: 0; width: 100%} .MathJax_SVG .MJX-monospace {font-family: monospace} .MathJax_SVG .MJX-sans-serif {font-family: sans-serif} .MathJax_SVG {display: inline; font-style: normal; font-weight: normal; line-height: normal; font-size: 100%; font-size-adjust: none; text-indent: 0; text-align: left; text-transform: none; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0; min-height: 0; border: 0; padding: 0; margin: 0} .MathJax_SVG * {transition: none; -webkit-transition: none; -moz-transition: none; -ms-transition: none; -o-transition: none} .mjx-svg-href {fill: blue; stroke: blue} .MathJax_SVG_LineBox {display: table!important} .MathJax_SVG_LineBox span {display: table-cell!important; width: 10000em!important; min-width: 0; max-width: none; padding: 0; border: 0; margin: 0}
haveaniceday
Sample Output 0
hae and via ecy
Explanation 0
, is between and .
Rewritten with rows and columns:
have
anic
eday
Sample Input 1
feedthedog
Sample Output 1
fto ehg ee dd
Explanation 1
, is between and .
Rewritten with rows and columns:
feed
thed
og
Sample Input 2
chillout
Sample Output 2
clu hlt io
Explanation 2
, is between and .
Rewritten with columns and rows ( so we have to use .)
chi
llo
ut
Solution in java8
Approach 1.
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String inputString = in.nextLine();
inputString = inputString.replaceAll("\\s+", "");
double col = Math.ceil(Math.pow(inputString.length(),0.5));
String answer ="";
for(int i=0;i<col;i++){
int j=0;
while(i+j<inputString.length()){
answer = answer + inputString.substring(i+j,i+j+1);
j=(int) (j+col);}
answer = answer + " ";}
System.out.println(answer);
}
}
Approach 2.
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s = in.nextLine();
double l = Math.sqrt(s.length());
int rows = (int) Math.floor(l);
int columns = (int) Math.ceil(l);
if(rows * columns < s.length()) {
rows++;
}
StringBuilder builder = new StringBuilder();
for(int i = 0; i < columns; i++) {
for(int j = 0; j < rows; j++) {
int index = (j * columns) + i;
if(index > s.length() - 1) {
rows--;
} else {
builder.append(s.charAt(index));
}
}
builder.append(" ");
}
System.out.println(builder.toString());
in.close();
}
}
Approach 3.
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String plaintext = scanner.next();
int numRows = (int)Math.floor(Math.sqrt(plaintext.length()));
int numCols = (int)Math.ceil(Math.sqrt(plaintext.length()));
if (numRows * numCols < plaintext.length()) {
numRows++;
}
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < numCols; i++) {
for (int j = 0; j < numRows; j++) {
int index = i + (numCols * j);
if (index < plaintext.length()) {
stringBuilder.append(plaintext.charAt(index));
}
}
stringBuilder.append(" ");
}
String ciphertext = stringBuilder.toString();
System.out.println(ciphertext);
}
}
Solution in python3
Approach 1.
a=input()
a=list(a)
l=len(a)**0.5
r=int(l)
c=r+1
if r==len(a)**0.5:
c=r
if r*c<len(a):
r=r+1
w=[]
q=0
for i in range(c):
b=q
for j in range(r):
w.append(a[q])
q+=c
if q>=len(a):
w.append(' ')
break
q=b+1
print(''.join(w))
Approach 2.
#!/bin/python3
import sys
import math
s = input().strip()
xc = math.ceil(math.sqrt(len(s)))
for i in range(xc):
for j in range(0, len(s), xc):
try:
print(s[i+j], end = '')
except:
pass
print(' ', end = '')
Approach 3.
#!/bin/python3
import sys
import math
s = input().strip()
floor = math.floor(math.sqrt(len(s)))
ceiling = math.ceil(math.sqrt(len(s)))
while (floor * ceiling < len(s)):
floor += 1
if (floor * ceiling < len(s)):
ceiling += 1
lst = []
for i in range(floor):
lst.append(s[:ceiling])
s = s[ceiling:]
result = ""
for i in range(ceiling):
for j in range(floor):
if i < len(lst[j]):
result += lst[j][i]
result += " "
print(result)
Solution in cpp
Approach 1.
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main() {
string phrase;
cin >> phrase;
float length = phrase.length();
int rows = floor(sqrt(length));
int cols = ceil(sqrt(length));
while(((cols - 1) * (rows) >= length) && (cols - 1 >= rows)){
--cols;
}
while(cols * rows < length){
++rows;
}
for(int i = 0; i < cols; ++i){
for(int j = 0; j < rows && (i + j * cols < length); ++j){
cout << phrase[i + (j * cols)];
}
cout << " ";
}
return 0;
}
Approach 2.
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int main(){
char s[1000];
gets(s);
int len = strlen(s);
int width, height;
int lo = floor(sqrt(len)), hi = ceil(sqrt(len));
int i, j;
bool found = false;
for(i = lo; i<=hi; i++){
for(j = i; j<=hi; j++){
if(i*j>=len) {
found = true;
break;
}
}
if(found) break;
}
width = j; height = i;
int index = 0;
for(i = 0; i<width; i++){
for(j = 0; j<height; j++){
index = j*width +i;
if(index<len) putchar(s[index]);
}
putchar(' ');
}
}
Approach 3.
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
string s;
int t=0;
cin>>s;
int n=s.length();
s.erase( std::remove_if( s.begin(), s.end(), ::isspace ), s.end() );
int r=floor(sqrt(n));int c=ceil(sqrt(n));
while((r*c)<n){
r=r+1;
}
char a[r][c];
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
a[i][j]=s[t];
t++;
}
}
for(int i=0;i<c;i++){
for(int j=0;j<r;j++){
if(isalpha(a[j][i]))
cout<<a[j][i];
}
cout<<" ";
}
return 0;
}