Dada una string str que consta de alfabetos ingleses en minúsculas. La tarea es cambiar cada carácter de la string con la tecla de la siguiente letra (en forma circular) en el teclado. Por ejemplo, ‘a’ se reemplaza con ‘s’ , ‘b’ se reemplaza con ‘n’ , ….., ‘l’ se reemplaza con ‘z’ , ….., ‘m’ se reemplaza con ‘q’ .
Ejemplos:
Entrada: str = «geeks»
Salida: hrrld
Entrada: str = «plmabc»
Salida: azqsnv
Enfoque: para cada carácter en minúscula del alfabeto inglés, inserte el carácter junto a él en el teclado en un mapa_desordenado . Ahora recorra la string dada carácter por carácter y actualice cada carácter con el mapa creado anteriormente.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; const string CHARS = "qwertyuiopasdfghjklzxcvbnm"; const int MAX = 26; // Function to return the modified string string getString(string str, int n) { // Map to store the next character // on the keyboard for every // possible lowercase character unordered_map<char, char> uMap; for (int i = 0; i < MAX; i++) { uMap[CHARS[i]] = CHARS[(i + 1) % MAX]; } // Update the string for (int i = 0; i < n; i++) { str[i] = uMap[str[i]]; } return str; } // Driver code int main() { string str = "geeks"; int n = str.length(); cout << getString(str, n); return 0; }
Java
// Java implementation of the approach import java.util.*; class GFG { static String CHARS = "qwertyuiopasdfghjklzxcvbnm"; static int MAX = 26; // Function to return the modified String static String getString(char[] str, int n) { // Map to store the next character // on the keyboard for every // possible lowercase character Map<Character, Character> uMap = new HashMap<>(); for (int i = 0; i < MAX; i++) { uMap. put(CHARS.charAt(i), CHARS.charAt((i + 1) % MAX)); } // Update the String for (int i = 0; i < n; i++) { str[i] = uMap.get(str[i]); } return String.valueOf(str); } // Driver code public static void main(String []args) { String str = "geeks"; int n = str.length(); System.out.println(getString(str.toCharArray(), n)); } } // This code is contributed by Rajput-Ji
Python3
# Python3 implementation of the approach CHARS = "qwertyuiopasdfghjklzxcvbnm"; MAX = 26; # Function to return the modified string def getString(string, n) : string = list(string); # Map to store the next character # on the keyboard for every # possible lowercase character uMap = {}; for i in range(MAX) : uMap[CHARS[i]] = CHARS[(i + 1) % MAX]; # Update the string for i in range(n) : string[i] = uMap[string[i]]; return "".join(string); # Driver code if __name__ == "__main__" : string = "geeks"; n = len(string); print(getString(string, n)); # This code is contributed by AnkitRai01
C#
// C# implementation of the approach using System; using System.Collections.Generic; class GFG { static String CHARS = "qwertyuiopasdfghjklzxcvbnm"; static int MAX = 26; // Function to return the modified String static String getString(char[] str, int n) { // Map to store the next character // on the keyboard for every // possible lowercase character 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]; } // Update the String for (int i = 0; i < n; i++) { str[i] = uMap[str[i]]; } return String.Join("", str); } // Driver code public static void Main(String []args) { String str = "geeks"; int n = str.Length; Console.WriteLine(getString(str.ToCharArray(), n)); } } // This code is contributed by PrinciRaj1992
Javascript
<script> // JavaScript implementation of the approach var CHARS = "qwertyuiopasdfghjklzxcvbnm"; var MAX = 26; // Function to return the modified string function getString(string, n) { var string = string.split(""); // Map to store the next character // on the keyboard for every // possible lowercase character uMap = []; for (let i = 0; i < MAX; i++) { uMap[CHARS[i]] = CHARS[(i + 1) % MAX]; } // Update the string for (let i = 0; i < n; i++) { string[i] = uMap[string[i]]; } return string.join(""); } // Driver code var string = "geeks"; var n = string.length; document.write(getString(string, n)); </script>
hrrld
Publicación traducida automáticamente
Artículo escrito por Akshita207 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA