Dada una string con alfabetos ingleses en minúsculas. La tarea es reemplazar todas las consonantes en la string con las vocales más cercanas. Si una consonante está cerca de dos vocales, reemplácela con la que aparece primero en los alfabetos ingleses.
Nota : Las vocales ya presentes en la string deben dejarse como están.
Ejemplos :
Input : str = "geeksforgeeks" Output : eeeiueooeeeiu Input : str = "gfg" Output : eee
Un enfoque simple es comparar la consonante con las vocales para determinar la vocal más cercana. Primero, comprueba si la consonante se encuentra entre dos vocales. Si cae entre 2 vocales, encuentre la diferencia absoluta entre el valor ASCII de la consonante con el valor ASCII de ambas vocales.
Reemplázalo con esa vocal, con la cual la diferencia absoluta es mínima.
Sin embargo, si el código ASCII de la consonante no se encuentra entre dos vocales, entonces la consonante puede ser ‘v’, ‘w’, ‘x’, ‘y’, ‘z’. Por lo tanto, la respuesta es ‘u’ en este caso.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to replace all consonants // with nearest vowels in a string #include <bits/stdc++.h> using namespace std; // Function to check if a character is // vowel or not bool isVowel(char ch) { if (ch != 'a' && ch != 'e' && ch != 'i' && ch != 'o' && ch != 'u') return false; return true; } // Function to replace consonant with // nearest vowels string replacingConsonants(string s) { for (int i = 0; i < s.length(); i++) { // if, string element is vowel, // jump to next element if (isVowel(s[i])) continue; // check if consonant lies between two vowels, // if it lies, than replace it with nearest vowel else { if (s[i] > 'a' && s[i] < 'e') { // here the absolute difference of // ascii value is considered if (abs(s[i] - 'a') > abs(s[i] - 'e')) s[i] = 'e'; else s[i] = 'a'; } else if (s[i] > 'e' && s[i] < 'i') { if (abs(s[i] - 'e') > abs(s[i] - 'i')) s[i] = 'i'; else s[i] = 'e'; } else if (s[i] > 'i' && s[i] < 'o') { if (abs(s[i] - 'i') > abs(s[i] - 'o')) s[i] = 'o'; else s[i] = 'i'; } else if (s[i] > 'o' && s[i] < 'u') { if (abs(s[i] - 'o') > abs(s[i] - 'u')) s[i] = 'u'; else s[i] = 'o'; } // when s[i] is equal to either // 'v', 'w', 'x', 'y', 'z' else if (s[i] > 'u') s[i] = 'u'; } } return s; } // Driver code int main() { string s = "geeksforgeeks"; cout << replacingConsonants(s); return 0; }
Java
// Java program to replace all consonants // with nearest vowels in a string import java.util.*; class Solution{ // Function to check if a character is // vowel or not static boolean isVowel(char ch) { if (ch != 'a' && ch != 'e' && ch != 'i' && ch != 'o' && ch != 'u') return false; return true; } // Function to replace consonant with // nearest vowels static String replacingConsonants(String s) { for (int i = 0; i < s.length(); i++) { // if, string element is vowel, // jump to next element if (isVowel(s.charAt(i))) continue; // check if consonant lies between two vowels, // if it lies, than replace it with nearest vowel else { if (s.charAt(i) > 'a' && s.charAt(i) < 'e') { // here the absolute difference of // ascii value is considered if (Math.abs(s.charAt(i) - 'a') > Math.abs(s.charAt(i) - 'e')) s = s.substring(0,i)+'e'+s.substring(i+1); else s= s.substring(0,i)+'a'+s.substring(i+1); } else if (s.charAt(i) > 'e' && s.charAt(i) < 'i') { if (Math.abs(s.charAt(i) - 'e') > Math.abs(s.charAt(i) - 'i')) s = s.substring(0,i)+'i'+s.substring(i+1); else s = s.substring(0,i)+'e'+s.substring(i+1); } else if (s.charAt(i) > 'i' && s.charAt(i) < 'o') { if (Math.abs(s.charAt(i) - 'i') > Math.abs(s.charAt(i) - 'o')) s= s.substring(0,i)+'o'+s.substring(i+1); else s= s.substring(0,i)+'i'+s.substring(i+1); } else if (s.charAt(i) > 'o' && s.charAt(i) < 'u') { if (Math.abs(s.charAt(i) - 'o') > Math.abs(s.charAt(i) - 'u')) s= s.substring(0,i)+'u'+s.substring(i+1); else s= s.substring(0,i)+'o'+s.substring(i+1); } // when s.charAt(i) is equal to either // 'v', 'w', 'x', 'y', 'z' else if (s.charAt(i) > 'u') s =s.substring(0,i)+'u'+s.substring(i+1); } } return s; } // Driver code public static void main(String args[]) { String s = "geeksforgeeks"; System.out.print( replacingConsonants(s)); } } //contributed by Arnab Kundu
Python3
# Python3 program to replace all consonants # with nearest vowels in a string # Function to check if a # character is vowel or not def isVowel(ch): if (ch != 'a' and ch != 'e' and ch != 'i' and ch != 'o' and ch != 'u'): return False return True # Function to replace consonant # with nearest vowels def replacingConsonants(s): for i in range(0, len(s)): # if, string element is vowel, # jump to next element if isVowel(s[i]): continue # check if consonant lies between two vowels, # if it lies, than replace it with nearest vowel else: if s[i] > 'a' and s[i] < 'e': # here the absolute difference of # ascii value is considered if (abs(ord(s[i]) - ord('a')) > abs(ord(s[i]) - ord('e'))): s[i] = 'e' else: s[i] = 'a' elif s[i] > 'e' and s[i] < 'i': if (abs(ord(s[i]) - ord('e')) > abs(ord(s[i]) - ord('i'))): s[i] = 'i' else: s[i] = 'e' elif (s[i] > 'i' and s[i] < 'o'): if (abs(ord(s[i]) - ord('i')) > abs(ord(s[i]) - ord('o'))): s[i] = 'o' else: s[i] = 'i' elif (s[i] > 'o' and s[i] < 'u'): if (abs(ord(s[i]) - ord('o')) > abs(ord(s[i]) - ord('u'))): s[i] = 'u' else: s[i] = 'o' # when s[i] is equal to either # 'v', 'w', 'x', 'y', 'z' elif (s[i] > 'u'): s[i] = 'u' return ''.join(s) # Driver code if __name__ == "__main__": s = "geeksforgeeks" print(replacingConsonants(list(s))) # This code is contributed by Rituraj Jain
C#
// C# program to replace all consonants // with nearest vowels in a string using System; public class Solution{ // Function to check if a character is // vowel or not static bool isVowel(char ch) { if (ch != 'a' && ch != 'e' && ch != 'i' && ch != 'o' && ch != 'u') return false; return true; } // Function to replace consonant with // nearest vowels static String replacingConsonants(String s) { for (int i = 0; i < s.Length; i++) { // if, string element is vowel, // jump to next element if (isVowel(s[i])) continue; // check if consonant lies between two vowels, // if it lies, than replace it with nearest vowel else { if (s[i] > 'a' && s[i] < 'e') { // here the absolute difference of // ascii value is considered if (Math.Abs(s[i] - 'a') > Math.Abs(s[i] - 'e')) s = s.Substring(0,i)+'e'+s.Substring(i+1); else s= s.Substring(0,i)+'a'+s.Substring(i+1); } else if (s[i] > 'e' && s[i] < 'i') { if (Math.Abs(s[i] - 'e') > Math.Abs(s[i] - 'i')) s = s.Substring(0,i)+'i'+s.Substring(i+1); else s = s.Substring(0,i)+'e'+s.Substring(i+1); } else if (s[i] > 'i' && s[i] < 'o') { if (Math.Abs(s[i] - 'i') > Math.Abs(s[i] - 'o')) s= s.Substring(0,i)+'o'+s.Substring(i+1); else s= s.Substring(0,i)+'i'+s.Substring(i+1); } else if (s[i] > 'o' && s[i] < 'u') { if (Math.Abs(s[i] - 'o') > Math.Abs(s[i] - 'u')) s= s.Substring(0,i)+'u'+s.Substring(i+1); else s= s.Substring(0,i)+'o'+s.Substring(i+1); } // when s[i] is equal to either // 'v', 'w', 'x', 'y', 'z' else if (s[i] > 'u') s =s.Substring(0,i)+'u'+s.Substring(i+1); } } return s; } // Driver code public static void Main() { String s = "geeksforgeeks"; Console.WriteLine( replacingConsonants(s)); } } // This code is contributed by PrinciRaj1992
Javascript
<script> // JavaScript program to replace all consonants // with nearest vowels in a string // Function to check if a character is // vowel or not function isVowel(ch) { if (ch !== "a" && ch !== "e" && ch !== "i" && ch !== "o" && ch !== "u") return false; return true; } // Function to replace consonant with // nearest vowels function replacingConsonants(s) { for (var i = 0; i < s.length; i++) { // if, string element is vowel, // jump to next element if (isVowel(s[i])) continue; // check if consonant lies between two vowels, // if it lies, than replace it with nearest vowel else { if (s[i] > "a" && s[i] < "e") { // here the absolute difference of // ascii value is considered if ( Math.abs(s[i].charCodeAt(0) - "a".charCodeAt(0)) > Math.abs(s[i].charCodeAt(0) - "e".charCodeAt(0)) ) s = s.substring(0, i) + "e" + s.substring(i + 1); else s = s.substring(0, i) + "a" + s.substring(i + 1); } else if (s[i] > "e" && s[i] < "i") { if ( Math.abs(s[i].charCodeAt(0) - "e".charCodeAt(0)) > Math.abs(s[i].charCodeAt(0) - "i".charCodeAt(0)) ) s = s.substring(0, i) + "i" + s.substring(i + 1); else s = s.substring(0, i) + "e" + s.substring(i + 1); } else if (s[i] > "i" && s[i] < "o") { if ( Math.abs(s[i].charCodeAt(0) - "i".charCodeAt(0)) > Math.abs(s[i].charCodeAt(0) - "o".charCodeAt(0)) ) s = s.substring(0, i) + "o" + s.substring(i + 1); else s = s.substring(0, i) + "i" + s.substring(i + 1); } else if (s[i] > "o" && s[i] < "u") { if ( Math.abs(s[i].charCodeAt(0) - "o".charCodeAt(0)) > Math.abs(s[i].charCodeAt(0) - "u".charCodeAt(0)) ) s = s.substring(0, i) + "u" + s.substring(i + 1); else s = s.substring(0, i) + "o" + s.substring(i + 1); } // when s[i] is equal to either // 'v', 'w', 'x', 'y', 'z' else if (s[i] > "u") s = s.substring(0, i) + "u" + s.substring(i + 1); } } return s; } // Driver code var s = "geeksforgeeks"; document.write(replacingConsonants(s)); </script>
eeeiueooeeeiu
Complejidad de tiempo: O(n), donde n es el tamaño de la string s
Espacio auxiliar: O(1)
Un mejor enfoque es hacer una array de tamaño 26 que almacene la vocal más cercana para cada carácter.
C++
// C++ program to replace all consonants // with nearest vowels in a string #include <bits/stdc++.h> using namespace std; // Function to replace consonant with // nearest vowels string replacingConsonants(string s) { char nVowel[] = "aaaeeeeiiiiioooooouuuuuuuu"; for (int i = 0; i < s.length(); i++) s[i] = nVowel[s[i] - 'a']; return s; } // Driver code int main() { string s = "geeksforgeeks"; cout << replacingConsonants(s); return 0; }
Java
// Java program to replace all consonants // with nearest vowels in a string import java.util.*; class solution { // Function to replace consonant with // nearest vowels static String replacingConsonants(String s) { String str = "aaaeeeeiiiiioooooouuuuuuuu"; char[] st = s.toCharArray(); for (int i = 0; i < s.length(); i++) { int index = st[i]-'a'; st[i] = str.charAt(index); } String str1 = new String(st); return str1; } // Driver code public static void main(String arr[]) { String s = "geeksforgeeks"; System.out.println(replacingConsonants(s)); } } // This code is contributed by Surendra_Gangwar
Python3
# Python3 program to replace all consonants # with nearest vowels in a string # Function to replace consonant with # nearest vowels def replacingConsonants(s): nVowel = "aaaeeeeiiiiioooooouuuuuuuu" for i in range (0, len(s)): s = s.replace(s[i], nVowel[ord(s[i]) - 97]) return s # Driver code s = "geeksforgeeks"; print(replacingConsonants(s)); # This code is contributed by # archana_kumari.
C#
// C# program to replace all consonants // with nearest vowels in a string using System; public class solution{ // Function to replace consonant with // nearest vowels static String replacingConsonants(String s) { String str = "aaaeeeeiiiiioooooouuuuuuuu"; char[] st = s.ToCharArray(); for (int i = 0; i < s.Length; i++) { int index = st[i]-'a'; st[i] = str[index]; } String str1 = new String(st); return str1; } // Driver code public static void Main() { String s = "geeksforgeeks"; Console.WriteLine(replacingConsonants(s)); } } // This code is contributed by 29AjayKumar
Javascript
<script> // Javascript program to replace all consonants // with nearest vowels in a string // Function to replace consonant with // nearest vowels function replacingConsonants(s) { var nVowel = "aaaeeeeiiiiioooooouuuuuuuu"; for (var i = 0; i < s.length; i++) s[i] = nVowel[s[i].charCodeAt(0) - 'a'.charCodeAt(0)]; return s.join(''); } // Driver code var s = "geeksforgeeks".split(''); document.write( replacingConsonants(s)); </script>
eeeiueooeeeiu
Complejidad de tiempo: O(n), donde n es el tamaño de la string s
Espacio auxiliar: O(26)
Publicación traducida automáticamente
Artículo escrito por Shashank_Sharma y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA