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:
$k -= (122 – $val);
$k = $k % 26;
$newS = $newS.chr(96 + $k);
$newS = $newS.chr($val + $k);
$k = $dup;
// print the new string
// Driver code
$str = “abc”;
$k = 28;
// function call
// This code is contributed by ita_c
Time Complexity: O(N), where N is the length of the string.
- Replace every character of a string by a different character
- Replace a character c1 with c2 and c2 with c1 in a string S
- Print the string after the specified character has occurred given no. of times
- Encrypt a string by repeating i-th character i times
- Check if max occurring character of one string appears same no. of times in other
- Decrypt a string encrypted by repeating i-th character i times
- Find a string such that every character is lexicographically greater than its immediate next character
- Map every character of one string to another such that all occurrences are mapped to the same character
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Minimize ASCII values sum after removing all occurrences of one character
- Total character pairs from two strings, with equal number of set bits in their ascii value
- Number of sub-strings that contain the given character exactly k times
- Count of substrings which contains a given character K times
- Longest subsequence where every character appears at-least k times
- Longest subsequence where each character occurs at least k times
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.