Given string str consisting of lowercase letters only and an integer k, the task is to replace every character of the given string with a character whose ASCII value is k times more than it. If the ASCII value exceeds ‘z’, then start checking from ‘a’ in a cyclic manner.
Examples:
Input: str = “abc”, k = 2
Output: cde
Explanation:
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
Output: cde
Explanation:
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 a new character.
Repeat the above steps for every character of the given string.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void encode(string s, int k){
string newS;
for ( int i=0; i<s.length(); ++i)
{
int val = int (s[i]);
int dup = k;
if (val + k > 122){
k -= (122-val);
k = k % 26;
newS += char (96 + k);
}
else
newS += char (val + k);
k = dup;
}
cout<<newS;
}
int main(){
string str = "abc" ;
int k = 28;
encode(str, k);
return 0;
}
|
Java
class GFG {
static void encode(String s, int k) {
String newS = "" ;
for ( int i = 0 ; i < s.length(); ++i) {
int val = s.charAt(i);
int dup = k;
if (val + k > 122 ) {
k -= ( 122 - val);
k = k % 26 ;
newS += ( char )( 96 + k);
} else {
newS += ( char )(val + k);
}
k = dup;
}
System.out.println(newS);
}
public static void main(String[] args) {
String str = "abc" ;
int k = 28 ;
encode(str, k);
}
}
|
Python3
def encode(s, k):
newS = ""
for i in range ( len (s)):
val = ord (s[i])
dup = k
if val + k> 122 :
k - = ( 122 - val)
k = k % 26
newS + = chr ( 96 + k)
else :
newS + = chr (val + k)
k = dup
print (newS)
str = "abc"
k = 28
encode( str , k)
|
C#
using System;
public class GFG {
static void encode(String s, int k) {
String newS = "" ;
for ( int i = 0; i < s.Length; ++i) {
int val = s[i];
int dup = k;
if (val + k > 122) {
k -= (122 - val);
k = k % 26;
newS += ( char )(96 + k);
} else {
newS += ( char )(96 + k);
}
k = dup;
}
Console.Write(newS);
}
public static void Main() {
String str = "abc" ;
int k = 28;
encode(str, k);
}
}
|
Javascript
<script>
function encode(s,k)
{
let newS = "" ;
for (let i = 0; i < s.length; ++i) {
let val = s[i].charCodeAt(0);
let dup = k;
if (val + k > 122) {
k -= (122 - val);
k = k % 26;
newS += String.fromCharCode(96 + k);
} else {
newS += String.fromCharCode(val + k);
}
k = dup;
}
document.write(newS);
}
let str = "abc" ;
let k = 28;
encode(str, k);
</script>
|
PHP
<?php
function encode( $s , $k )
{
$newS = "" ;
for ( $i = 0; $i < strlen ( $s ); ++ $i )
{
$val = ord( $s [ $i ]);
$dup = $k ;
if ( $val + $k > 122)
{
$k -= (122 - $val );
$k = $k % 26;
$newS = $newS . chr (96 + $k );
}
else
$newS = $newS . chr ( $val + $k );
$k = $dup ;
}
echo $newS ;
}
$str = "abc" ;
$k = 28;
encode( $str , $k );
?>
|
Complexity Analysis:
- Time Complexity: O(N), as we are using a loop to traverse N times so it will cost us O(N) time, where N is the length of the string.
- Auxiliary Space: O(N)
Approach: Using modular arithmetic
Steps:
- Convert the input string to a list of characters.
- Iterate over each character in the list.
- Calculate the new ASCII value by adding k to the ASCII value of the current character.
- If the new ASCII value exceeds the ASCII value of ‘z’, wrap around to ‘a’ by subtracting 26.
- Replace the current character in the list with the character corresponding to the new ASCII value.
- Convert the list of characters back to a string
- Return it as the result.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <string>
using namespace std;
string replaceCharacters(string str, int k) {
for ( int i = 0; i < str.length(); ++i) {
int asciiVal = (str[i] - 'a' + k) % 26 + 'a' ;
str[i] = static_cast < char >(asciiVal);
}
return str;
}
int main() {
string inputStr = "abc" ;
int kValue = 2;
string outputStr = replaceCharacters(inputStr, kValue);
cout << outputStr << endl;
return 0;
}
|
Java
public class GFG {
public static String replaceCharacters(String str, int k) {
char [] charArray = str.toCharArray();
for ( int i = 0 ; i < charArray.length; ++i) {
int asciiVal = (charArray[i] - 'a' + k) % 26 + 'a' ;
charArray[i] = ( char ) asciiVal;
}
return new String(charArray);
}
public static void main(String[] args) {
String inputStr = "abc" ;
int kValue = 2 ;
String outputStr = replaceCharacters(inputStr, kValue);
System.out.println(outputStr);
}
}
|
Python
def replace_characters(string, k):
char_list = list (string)
for i in range ( len (char_list)):
ascii_val = ( ord (char_list[i]) - ord ( 'a' ) + k) % 26 + ord ( 'a' )
char_list[i] = chr (ascii_val)
return "".join(char_list)
input_str = "abc"
k_value = 2
output_str = replace_characters(input_str, k_value)
print (output_str)
|
Time Complexity: O(n), where n is the length of the input string.
Auxiliary Space: O(n)