An Encryption algorithm works in the following way
Message: eNEMYwILLaTTACK
Enrypted Form: eYLA NwaC EITK MLT
The way to do it is that the number of rows and the number of columns in the figure (formed from the alphabets of the Message) lie between floor (sqrt(len(message))) and ceil (sqrt(len(message))). It also states that the number of rows is less than or equal to the number of columns, and that the area of rectangle thus formed is minimum. Based on the this criteria, we have to choose a set of values for rows and columns.
For the string haveaniceday, we have floor(sqrt(len(message))) = 3 and ceil(sqrt(len(message))) = 4.
3 * 3 = 9 < len(message) = 15
3 * 4 = 12 = len(message)
4 * 3 = 12 = len(message)
4 * 4 = 16 > len(message)
Out of the 4 possible squares, we can see that, rows = 3 and columns = 4 is the best fit.
On building the figure, we get
have
anic
eday
So, the Encrypted form is "hae and via ecy".
 
Example 1:
Input:
S = "eNEMYwILLaTTACK"
Output:
eYLA NwaC EITK MLT
Explanation:
The encrypted form of the given String
is printed.
Example 2:
Input:
S = "SavetheMines"
Output:
Sti ahn vee eMs
Explanation:
The encrypted form of the given String
is printed.
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function encryptString() which takes an String S as input and returns the encrypted form as a String.
 
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(1)
 
Constraints:
1 <= |S| <= 10^{5}

Source: geeksforgeeks | Difficulty: EASY
Tags: Matrices

Data structures

Starter code: #User function Template for python3



class Solution:

    def encryptString(self, S):

        # code here 

Solutions:
import math

class Solution:

	def encryptString(self, S):
		c = int(math.ceil(math.sqrt(len(S))))
		r = int(math.sqrt(len(S)))
		if len(S) == 1:
			return S
		if r * c < len(S):
			r += 1
		encryp = ''
		for i in range(c):
			for j in range(i, len(S), c):
				encryp += S[j]
			encryp += ' '
		return encryp


import math

class Solution:

	def encryptString(self, S):
		col = math.ceil(math.sqrt(len(S)))
		st = ''
		for i in range(col):
			for j in range(i, len(S), col):
				st += S[j]
			st += ' '
		return st


import math

class Solution:

	def encryptString(self, S):
		m = math.ceil(math.sqrt(len(S)))
		grid = []
		while S:
			grid.append([*S[:m]])
			S = S[m:]
		grid[-1] += [' '] * (m - len(grid[-1]))
		grid += [[' '] * m]
		output = ''
		for j in range(m):
			for row in grid:
				output += row[j]
		return output


import math

class Solution:

	def encryptString(self, S):
		l1 = math.floor(math.sqrt(len(S)))
		l2 = math.ceil(math.sqrt(len(S)))
		res = ''
		steps = 0
		if l1 * l1 == len(S):
			steps = l1
		else:
			steps = l2
		for i in range(steps):
			for j in range(i, len(S), steps):
				res += S[j]
			res += ' '
		return res


import math

class Solution:

	def encryptString(self, S):
		floor = math.floor(math.sqrt(len(S)))
		ceiling = math.ceil(math.sqrt(len(S)))
		j = 0
		st = ''
		while j < ceiling:
			for i in range(j, len(S), ceiling):
				st += S[i]
			j += 1
			if j < len(S):
				st = st + ' '
		return st


import math
import sys

class Solution:

	def encryptString(self, S):
		string = ''
		x = 'aman'
		a = 0
		b = 0
		rows = math.floor(math.sqrt(len(S)))
		columns = math.ceil(math.sqrt(len(S)))
		if rows * columns < len(S):
			rows = rows + 1
		mat = [[x for i in range(columns)] for j in range(rows)]
		for i in range(rows):
			for j in range(columns):
				if a >= len(S):
					break
				mat[i][j] = S[a]
				a += 1
		for j in range(columns):
			for i in range(rows):
				if mat[i][j] == x:
					continue
				if b >= len(S):
					break
				string = string + str(mat[i][j])
				b = b + 1
			string = string + ' '
		return string