Dada una string con caracteres repetidos, tenemos que insertar una estrella, es decir, «*» entre un par de caracteres idénticos adyacentes usando la recursividad.
Ejemplos:
Input : aabb Output : a*ab*b Input : xxxy Output : x*x*xy
Acercarse:
- Si hay una string vacía, simplemente regrese. Esto forma nuestra condición base .
- Compruebe si los dos primeros caracteres son idénticos. En caso afirmativo, inserte «*» entre ellos.
- Como ahora hemos verificado caracteres idénticos en las dos primeras posiciones de la string, ahora hacemos una llamada recursiva sin el primer carácter de la string .
El enfoque anterior se ha implementado a continuación:
C++
// Recursive CPP program to insert * between // two consecutive same characters. #include <iostream> using namespace std; // Function to insert * at desired position void pairStar(string& input, string& output, int i = 0) { // Append current character output = output + input[i]; // If we reached last character if (i == input.length() - 1) return; // If next character is same, // append '*' if (input[i] == input[i + 1]) output = output + '*'; pairStar(input, output, i+1); } // Driver code int main() { string input = "geeks", output = ""; pairStar(input, output); cout << output << endl; return 0; }
Java
// Recursive Java program to insert * between // two consecutive same characters. class GFG { static String output=""; // Function to insert * at desired position static void pairStar(String input, int i) { // Append current character output = output + input.charAt(i); // If we reached last character if (i == input.length() - 1) return; // If next character is same, // append '*' if (input.charAt(i) == input.charAt(i+1)) output = output + '*'; pairStar(input, i+1); } // Driver code public static void main(String[] args) { String input = "geeks"; pairStar(input,0); System.out.println(output); } } // This code is contributed by Princi Singh
Python3
# Recursive CPP program to insert * between # two consecutive same characters. # Function to insert * at desired position def pairStar(Input, Output, i = 0) : # Append current character Output = Output + Input[i] # If we reached last character if (i == len(Input) - 1) : print(Output) return; # If next character is same, # append '*' if (Input[i] == Input[i + 1]) : Output = Output + '*'; pairStar(Input, Output, i + 1); # Driver code if __name__ == "__main__" : Input = "geeks" Output = "" pairStar(Input, Output); # This code is contributed by Ryuga
C#
// Recursive C# program to insert * between // two consecutive same characters. using System; class GFG { static String output=""; // Function to insert * at desired position static void pairStar(String input, int i) { // Append current character output = output + input[i]; // If we reached last character if (i == input.Length - 1) return; // If next character is same, // append '*' if (input[i] == input[i+1]) output = output + '*'; pairStar(input, i+1); } // Driver code public static void Main(String[] args) { String input = "geeks"; pairStar(input,0); Console.WriteLine(output); } } /* This code is contributed by PrinciRaj1992 */
PHP
<?php // Recursive PHP program to insert * between // two consecutive same characters. // Function to insert * at desired position function pairStar(&$input, &$output, $i = 0) { // Append current character $output = $output . $input[$i]; // If we reached last character if ($i == strlen($input) - 1) return; // If next character is same, // append '*' if ($input[$i] == $input[$i + 1]) $output = $output . '*'; pairStar($input, $output, $i+1); } // Driver code $input = "geeks"; $output = ""; pairStar($input, $output); echo $output; return 0; // This code is contributed by ChitraNayal ?>
Javascript
<script> // Recursive Javascript program to insert * between // two consecutive same characters. let output=""; // Function to insert * at desired position function pairStar(input,i) { // Append current character output = output + input[i]; // If we reached last character if (i == input.length - 1) return; // If next character is same, // append '*' if (input[i] == input[i+1]) output = output + '*'; pairStar(input, i+1); } // Driver code let input = "geeks"; pairStar(input,0); document.write(output); // This code is contributed by avanitrachhadiya2155 </script>
Producción:
ge*eks
Nota: La función recursiva en el código anterior es recursiva de cola ya que la llamada recursiva es lo último que ejecuta la función.
Publicación traducida automáticamente
Artículo escrito por lakshaygupta2807 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA