📜  在一对相同字符之间插入星号的递归程序

📅  最后修改于: 2021-05-04 13:38:50             🧑  作者: Mango

给定一个包含重复字符的字符串,我们必须使用递归在一对相邻的相同字符之间插入星号,即“ *”

例子:

Input : aabb 
Output : a*ab*b

Input : xxxy
Output : x*x*xy

方法:

  • 如果有一个空字符串,则只需返回即可。这构成了我们的基本条件

    在其他方面,我们执行以下操作-

  • 检查前两个字符是否相同。如果是,则在它们之间插入“ *”。
  • 由于我们现在已经在字符串的前两个位置检查了相同的字符,因此我们现在不使用字符串的第一个字符进行递归调用。

上述方法已在以下实现:

C++
// Recursive CPP program to insert * between
// two consecutive same characters.
#include 
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


输出:
ge*eks

注意:上面代码中的递归函数是尾递归,因为递归调用是该函数执行的最后一件事。

想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”