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:

## Java

`// Java implementation of the above approach: ` `public` `class` `GFG { ` ` ` ` ` `static` `final` `int` `MAX = ` `26` `; ` ` ` ` ` `// Function to return the encrypted string ` ` ` `static` `String encryptStr(String str, ` `int` `n, ` `int` `x) ` ` ` `{ ` ` ` ` ` `// Reduce x because rotation of ` ` ` `// length 26 is unnecessary ` ` ` `x = x % MAX; ` ` ` `char` `arr[] = str.toCharArray(); ` ` ` ` ` `// calculate the frequency of characters ` ` ` `int` `freq[] = ` `new` `int` `[MAX]; ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `freq[arr[i] - ` `'a'` `]++; ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) { ` ` ` ` ` `// If the frequency of current character ` ` ` `// is even then increment it by x ` ` ` `if` `(freq[arr[i] - ` `'a'` `] % ` `2` `== ` `0` `) { ` ` ` `int` `pos = (arr[i] - ` `'a'` `+ x) % MAX; ` ` ` `arr[i] = (` `char` `)(pos + ` `'a'` `); ` ` ` `} ` ` ` ` ` `// Else decrement it by x ` ` ` `else` `{ ` ` ` `int` `pos = (arr[i] - ` `'a'` `- x); ` ` ` `if` `(pos < ` `0` `) ` ` ` `pos += MAX; ` ` ` `arr[i] = (` `char` `)(pos + ` `'a'` `); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Return the count ` ` ` `return` `String.valueOf(arr); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `String s = ` `"abcda"` `; ` ` ` `int` `n = s.length(); ` ` ` `int` `x = ` `3` `; ` ` ` `System.out.println(encryptStr(s, n, x)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python

`# Python3 implementation of the above approach: ` `MAX` `=` `26` ` ` `# Function to return the encrypted strring ` `def` `encryptstrr(strr, n, x): ` ` ` ` ` `# Reduce x because rotation of ` ` ` `# length 26 is unnecessary ` ` ` `x ` `=` `x ` `%` `MAX` ` ` `arr ` `=` `list` `(strr) ` ` ` ` ` `# calculate the frequency of characters ` ` ` `freq ` `=` `[` `0` `]` `*` `MAX` ` ` `for` `i ` `in` `range` `(n): ` ` ` `freq[` `ord` `(arr[i]) ` `-` `ord` `(` `'a'` `)] ` `+` `=` `1` ` ` ` ` `for` `i ` `in` `range` `(n): ` ` ` ` ` `# If the frequency of current character ` ` ` `# is even then increment it by x ` ` ` `if` `(freq[` `ord` `(arr[i]) ` `-` `ord` `(` `'a'` `)] ` `%` `2` `=` `=` `0` `): ` ` ` `pos ` `=` `(` `ord` `(arr[i]) ` `-` `ord` `(` `'a'` `) ` `+` `x) ` `%` `MAX` ` ` `arr[i] ` `=` `chr` `(pos ` `+` `ord` `(` `'a'` `)) ` ` ` ` ` `# Else decrement it by x ` ` ` `else` `: ` ` ` `pos ` `=` `(` `ord` `(arr[i]) ` `-` `ord` `(` `'a'` `) ` `-` `x) ` ` ` `if` `(pos < ` `0` `): ` ` ` `pos ` `+` `=` `MAX` ` ` `arr[i] ` `=` `chr` `(pos ` `+` `ord` `(` `'a'` `)) ` ` ` ` ` `# Return the count ` ` ` `return` `"".join(arr) ` ` ` ` ` `# Driver code ` `s ` `=` `"abcda"` `n ` `=` `len` `(s) ` `x ` `=` `3` `print` `(encryptstrr(s, n, x)) ` ` ` `# This code is contributed by ` `# shubhamsingh10 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the above approach: ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `static` `int` `MAX = 26; ` ` ` ` ` `// Function to return the encrypted string ` ` ` `public` `static` `char` `[] encryptStr(String str, ` ` ` `int` `n, ` `int` `x) ` ` ` `{ ` ` ` ` ` `// Reduce x because rotation of ` ` ` `// length 26 is unnecessary ` ` ` `x = x % MAX; ` ` ` `char` `[] arr = str.ToCharArray(); ` ` ` ` ` `// calculate the frequency of characters ` ` ` `int` `[] freq = ` `new` `int` `[MAX]; ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `freq[arr[i] - ` `'a'` `]++; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` ` ` `// If the frequency of current character ` ` ` `// is even then increment it by x ` ` ` `if` `(freq[arr[i] - ` `'a'` `] % 2 == 0) ` ` ` `{ ` ` ` `int` `pos = (arr[i] - ` `'a'` `+ x) % MAX; ` ` ` `arr[i] = (` `char` `)(pos + ` `'a'` `); ` ` ` `} ` ` ` ` ` `// Else decrement it by x ` ` ` `else` ` ` `{ ` ` ` `int` `pos = (arr[i] - ` `'a'` `- x); ` ` ` `if` `(pos < 0) ` ` ` `pos += MAX; ` ` ` `arr[i] = (` `char` `)(pos + ` `'a'` `); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Return the count ` ` ` `return` `arr; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main(String[] args) ` ` ` `{ ` ` ` `String s = ` `"abcda"` `; ` ` ` `int` `n = s.Length; ` ` ` `int` `x = 3; ` ` ` `Console.WriteLine(encryptStr(s, n, x)); ` ` ` `} ` `} ` ` ` `// This code is contributed by ` `// sanjeev2552 ` |

*chevron_right*

*filter_none*

**Output:**

dyzad

** Time Complexity:** O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Encrypt a string into the Rovarspraket (The Robber Language)
- Encrypt string with product of number of vowels and consonants in substring of size k
- Encrypt a string by repeating i-th character i times
- Program to Encrypt a String using ! and @
- Construct a binary string following the given constraints
- Restore original String from given Encrypted String by the given operations
- Find the final co-ordinates reached by following a sequence of directions
- Minimum number of given operations required to convert a string to another string
- Minimum given operations required to convert a given binary string to all 1's
- Minimum reduce operations to convert a given string into a palindrome
- Final string after performing given operations
- Maximum number of given operations to remove the entire string
- Minimum operations required to make the string satisfy the given condition
- Minimum number of operations required to obtain a given Binary String
- Minimum operations required to convert all characters of a String to a given Character
- Find value after N operations to remove N characters of string S with given constraints
- Minimum operations to transform given string to another by moving characters to front or end
- Kth character from the Nth string obtained by the given operations
- Given a string and an integer k, find the kth sub-string when all the sub-strings are sorted according to the given condition
- Count of operations to make a binary string"ab" free

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.