Modify the string such that every character gets replaced with the next character in the keyboard
Given a string str consisting of lowercase English alphabets. The task is to change each character of the string with the next letter (in a circular fashion) key in the keyboard. For example, ‘a’ gets replaced with ‘s’, ‘b’ gets replaced with ‘n’, ….., ‘l’ gets replaced with ‘z’, ….., ‘m’ gets replaced with ‘q’.
Examples:
Input: str = “geeks”
Output: hrrld
Input: str = “plmabc”
Output: azqsnv
Approach: For every lowercase character of the English alphabet, insert the character next to it in the keyboard in an unordered_map. Now traverse the given string character by character, and update every character with the map created earlier.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
const string CHARS = "qwertyuiopasdfghjklzxcvbnm" ;
const int MAX = 26;
string getString(string str, int n)
{
unordered_map< char , char > uMap;
for ( int i = 0; i < MAX; i++) {
uMap[CHARS[i]] = CHARS[(i + 1) % MAX];
}
for ( int i = 0; i < n; i++) {
str[i] = uMap[str[i]];
}
return str;
}
int main()
{
string str = "geeks" ;
int n = str.length();
cout << getString(str, n);
return 0;
}
|
Java
import java.util.*;
class GFG {
static String CHARS = "qwertyuiopasdfghjklzxcvbnm" ;
static int MAX = 26 ;
static String getString( char [] str, int n)
{
Map<Character, Character> uMap = new HashMap<>();
for ( int i = 0 ; i < MAX; i++) {
uMap.put(CHARS.charAt(i),
CHARS.charAt((i + 1 ) % MAX));
}
for ( int i = 0 ; i < n; i++) {
str[i] = uMap.get(str[i]);
}
return String.valueOf(str);
}
public static void main(String[] args)
{
String str = "geeks" ;
int n = str.length();
System.out.println(getString(str.toCharArray(), n));
}
}
|
Python3
CHARS = "qwertyuiopasdfghjklzxcvbnm"
MAX = 26
def getString(string, n):
string = list (string)
uMap = {}
for i in range ( MAX ):
uMap[CHARS[i]] = CHARS[(i + 1 ) % MAX ]
for i in range (n):
string[i] = uMap[string[i]]
return "".join(string)
if __name__ = = "__main__" :
string = "geeks"
n = len (string)
print (getString(string, n))
|
C#
using System;
using System.Collections.Generic;
class GFG {
static String CHARS = "qwertyuiopasdfghjklzxcvbnm" ;
static int MAX = 26;
static String getString( char [] str, int n)
{
Dictionary< char , char > uMap
= new Dictionary< char , char >();
for ( int i = 0; i < MAX; i++) {
if (!uMap.ContainsKey(CHARS[i]))
uMap.Add(CHARS[i], CHARS[(i + 1) % MAX]);
else
uMap[CHARS[i]] = CHARS[(i + 1) % MAX];
}
for ( int i = 0; i < n; i++) {
str[i] = uMap[str[i]];
}
return String.Join( "" , str);
}
public static void Main(String[] args)
{
String str = "geeks" ;
int n = str.Length;
Console.WriteLine(getString(str.ToCharArray(), n));
}
}
|
Javascript
<script>
var CHARS = "qwertyuiopasdfghjklzxcvbnm" ;
var MAX = 26;
function getString(string, n) {
var string = string.split( "" );
uMap = [];
for (let i = 0; i < MAX; i++) {
uMap[CHARS[i]] = CHARS[(i + 1) % MAX];
}
for (let i = 0; i < n; i++) {
string[i] = uMap[string[i]];
}
return string.join( "" );
}
var string = "geeks" ;
var n = string.length;
document.write(getString(string, n));
</script>
|
Time complexity: O(n) where n is the length of the given string
Auxiliary space: O(1)
Last Updated :
01 Nov, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...