Given a string str consisting of lowercase letters only and an integer k, the task is to replace every character of the given string by character whose ASCII value is k times more than it. If ASCII value exceeds ‘z’, then start checking from ‘a’ in a cyclic manner.
Input: str = “abc”, k = 2
a is moved by 2 times which results in character c
b is moved by 2 times which results in character d
c is moved by 2 times which results in character e
Input: str = “abc”, k = 28
a is moved 25 times, z is reached. Then 26th character will be a, 27-th b and 28-th c.
b is moved 24 times, z is reached. 28-th is d.
b is moved 23 times, z is reached. 28-th is e.
Approach: Iterate for every character in the string and perform the below steps for each character:
- Add k to the ASCII value of character str[i].
- If it exceeds 122, then perform a modulus operation of k with 26 to reduce the number of steps, as 26 is the maximum number of shifts that can be performed in a rotation.
- To find the character, add k to 96. Hence the character with ASCII value k+96 will be the new character.
Repeat the above steps for every character of the given string.
Below is the implementation of the above approach:
Time Complexity: O(N), where N is the length of the string.
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.
- Count of alphabets having ASCII value less than and greater than k
- Count the number of words having sum of ASCII values less than and greater than k
- Remove characters that appear more than k times
- Remove elements from the array which appear more than k times
- Find number of substrings of length k whose sum of ASCII value of characters is divisible by k
- Sub-strings having exactly k characters that have ASCII value greater than p
- Count of alphabets whose ASCII values can be formed with the digits of N
- Program to replace every space in a string with hyphen
- Count and Print the alphabets having ASCII value in the range [l, r]
- Count and Print the alphabets having ASCII value not in the range [l, r]
- Remove characters from string that appears strictly less than K times
- Replace every element of the array with BitWise XOR of all other
- Replace every element with the greatest element on its left side
- Replace every elements in the array by its frequency in the array
- Replace every element of the array by product of all other elements
- Replace every array element by sum of previous and next
- Replace every array element by Bitwise Xor of previous and next element
- Replace every element with the smallest element on its left side
- Replace every element of the array by its previous element
- Replace every element of the array by its next element
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.