Encrypt the given string with the following operations
Last Updated :
22 Dec, 2022
Given a string s, the task is to encrypt the string in the following way:
- If the frequency of current character is even, then increment current character by x.
- If the frequency of current character is odd, then decrement current character by x.
Note: All the operations are circular that is adding 1 to ‘z’ will give ‘a’ and subtracting 1 from ‘a’ will give ‘z’
Examples:
Input :s=”abcda”, x=3
Output :dyzad
‘a’ appear 2 times in the string, hence incrementing ‘a’ by 3 becomes ‘d’
‘b’ appear 1 times in the string, hence decrementing ‘b’ by 3 becomes ‘y’
‘c’ appear 1 times in the string, hence decrementing ‘c’ by 3 becomes ‘z’
‘d’ appear 1 times in the string, hence decrementing ‘d’ by 3 becomes ‘a’
‘a’ appear 2 times in the string, hence incrementing ‘a’ by 3 becomes ‘d’
Hence the string becomes “dyzad”
Input :s=”aabbc”, x=5
Output :ffggx
Approach:
- Create a frequency array to store the frequency of each character.
- Traverse the given string and for every character, if its frequency is even, then increment it by x else if the frequency is odd decrement it by x.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#define MAX 26
using namespace std;
string encryptStr(string str, int n, int x)
{
x = x % MAX;
int freq[MAX] = {0};
for ( int i = 0; i < n; i++)
{
freq[str[i] - 'a' ]++;
}
for ( int i = 0; i < n; i++)
{
if (freq[str[i] - 'a' ] % 2 == 0)
{
int pos = (str[i] - 'a' + x) % MAX;
str[i] = ( char )(pos + 'a' );
}
else
{
int pos = (str[i] - 'a' - x);
if (pos < 0)
{
pos += MAX;
}
str[i] = ( char )(pos + 'a' );
}
}
return str;
}
int main()
{
string s = "abcda" ;
int n = s.size();
int x = 3;
cout << encryptStr(s, n, x) << endl;
return 0;
}
|
Java
public class GFG {
static final int MAX = 26 ;
static String encryptStr(String str, int n, int x)
{
x = x % MAX;
char arr[] = str.toCharArray();
int freq[] = new int [MAX];
for ( int i = 0 ; i < n; i++)
freq[arr[i] - 'a' ]++;
for ( int i = 0 ; i < n; i++) {
if (freq[arr[i] - 'a' ] % 2 == 0 ) {
int pos = (arr[i] - 'a' + x) % MAX;
arr[i] = ( char )(pos + 'a' );
}
else {
int pos = (arr[i] - 'a' - x);
if (pos < 0 )
pos += MAX;
arr[i] = ( char )(pos + 'a' );
}
}
return String.valueOf(arr);
}
public static void main(String[] args)
{
String s = "abcda" ;
int n = s.length();
int x = 3 ;
System.out.println(encryptStr(s, n, x));
}
}
|
Python3
MAX = 26
def encryptstrr(strr, n, x):
x = x % MAX
arr = list (strr)
freq = [ 0 ] * MAX
for i in range (n):
freq[ ord (arr[i]) - ord ( 'a' )] + = 1
for i in range (n):
if (freq[ ord (arr[i]) - ord ( 'a' )] % 2 = = 0 ):
pos = ( ord (arr[i]) - ord ( 'a' ) + x) % MAX
arr[i] = chr (pos + ord ( 'a' ))
else :
pos = ( ord (arr[i]) - ord ( 'a' ) - x)
if (pos < 0 ):
pos + = MAX
arr[i] = chr (pos + ord ( 'a' ))
return "".join(arr)
s = "abcda"
n = len (s)
x = 3
print (encryptstrr(s, n, x))
|
C#
using System;
class GFG
{
static int MAX = 26;
public static char [] encryptStr(String str,
int n, int x)
{
x = x % MAX;
char [] arr = str.ToCharArray();
int [] freq = new int [MAX];
for ( int i = 0; i < n; i++)
freq[arr[i] - 'a' ]++;
for ( int i = 0; i < n; i++)
{
if (freq[arr[i] - 'a' ] % 2 == 0)
{
int pos = (arr[i] - 'a' + x) % MAX;
arr[i] = ( char )(pos + 'a' );
}
else
{
int pos = (arr[i] - 'a' - x);
if (pos < 0)
pos += MAX;
arr[i] = ( char )(pos + 'a' );
}
}
return arr;
}
public static void Main(String[] args)
{
String s = "abcda" ;
int n = s.Length;
int x = 3;
Console.WriteLine(encryptStr(s, n, x));
}
}
|
Javascript
<script>
var MAX = 26;
function encryptStr(str, n, x)
{
x = x % MAX;
var freq = Array(MAX).fill(0);
for ( var i = 0; i < n; i++)
{
freq[str[i].charCodeAt(0) - 'a' .charCodeAt(0)]++;
}
for ( var i = 0; i < n; i++)
{
if (freq[str[i].charCodeAt(0) - 'a' .charCodeAt(0)] % 2 == 0)
{
var pos = (str[i].charCodeAt(0) - 'a' .charCodeAt(0) + x) % MAX;
str[i] = String.fromCharCode(pos + 'a' .charCodeAt(0));
}
else
{
var pos = (str[i].charCodeAt(0) - 'a' .charCodeAt(0) - x);
if (pos < 0)
{
pos += MAX;
}
str[i] = String.fromCharCode(pos + 'a' .charCodeAt(0));
}
}
return str.join( '' );
}
var s = "abcda" ;
var n = s.length;
var x = 3;
document.write( encryptStr(s.split( '' ), n, x));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...