📌  相关文章
📜  删除字符串所有出现的字符|递归方法

📅  最后修改于: 2021-09-06 05:54:27             🧑  作者: Mango

给定字符串str ,任务是编写一个递归程序来删除字符串中所有出现的字符X

例子:

迭代方法:可以在这篇文章中找到解决这个问题的迭代方法。
递归方法:以下是步骤:

  1. 获取要删除字符X的字符串str和字符X。
  2. 递归迭代字符串中的所有字符:
    • 基本情况:如果递归调用的字符串str的长度为 0,则从函数返回空字符串。
if(str.length()==0) {
   return "";
}
  • 递归调用:如果不满足基本情况,则检查第 0 个索引处的字符是否为 X,然后递归迭代删除第一个字符的子字符串。
if (str[0] == X) {
        return recursive_function(str.substr(1), X);
}
  • 返回语句:在每次递归调用时(除了基本情况和上述条件),返回下一次迭代的递归函数,包括第 0 个索引处的字符。
return str[0] + recursive_function(str.substr(1), X)

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to remove all occurrences
// of a character in the string
string removeCharRecursive(string str,
                           char X)
{
    // Base Case
    if (str.length() == 0) {
        return "";
    }
 
    // Check the first character
    // of the given string
    if (str[0] == X) {
 
        // Pass the rest of the string
        // to recursion Function call
        return removeCharRecursive(str.substr(1), X);
    }
 
    // Add the first character of str
    // and string from recursion
    return str[0]
           + removeCharRecursive(str.substr(1), X);
}
 
// Driver Code
int main()
{
    // Given String
    string str = "geeksforgeeks";
 
    // Given character
    char X = 'e';
 
    // Function Call
    str = removeCharRecursive(str, X);
    cout << str;
    return 0;
}


Java
// Java program for the above approach
class GFG{
     
// Function to remove all occurrences
// of a character in the string
static String removeCharRecursive(String str,
                                  char X)
{
     
    // Base Case
    if (str.length() == 0)
    {
        return "";
    }
 
    // Check the first character
    // of the given string
    if (str.charAt(0) == X)
    {
 
        // Pass the rest of the string
        // to recursion Function call
        return removeCharRecursive(
               str.substring(1), X);
    }
 
    // Add the first character of str
    // and string from recursion
    return str.charAt(0) +
           removeCharRecursive(
           str.substring(1), X);
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given String
    String str = "geeksforgeeks";
 
    // Given character
    char X = 'e';
 
    // Function call
    str = removeCharRecursive(str, X);
     
    System.out.println(str);
}
}
 
// This code is contributed by jrishabh99


Python3
# Python3 program for the above approach
 
# Function to remove all occurrences
# of a character in the string
def removeCharRecursive(str, X):
     
    # Base Case
    if (len(str) == 0):
        return ""
     
    # Check the first character
    # of the given string
    if (str[0] == X):
 
        # Pass the rest of the string
        # to recursion Function call
        return removeCharRecursive(str[1:], X)
     
    # Add the first character of str
    # and string from recursion
    return str[0] + removeCharRecursive(str[1:], X)
 
# Driver Code
 
# Given String
str = "geeksforgeeks"
 
# Given character
X = 'e'
 
# Function call
str = removeCharRecursive(str, X)
 
print(str)
 
# This code is contributed by sanjoy_62


C#
// C# program for the above approach
using System;
 
class GFG{
     
// Function to remove all occurrences
// of a character in the string
static String removeCharRecursive(String str,
                                  char X)
{
     
    // Base Case
    if (str.Length == 0)
    {
        return "";
    }
 
    // Check the first character
    // of the given string
    if (str[0] == X)
    {
 
        // Pass the rest of the string
        // to recursion Function call
        return removeCharRecursive(
            str.Substring(1), X);
    }
 
    // Add the first character of str
    // and string from recursion
    return str[0] + removeCharRecursive(
                    str.Substring(1), X);
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given String
    String str = "geeksforgeeks";
 
    // Given character
    char X = 'e';
 
    // Function call
    str = removeCharRecursive(str, X);
     
    Console.WriteLine(str);
}
}
 
// This code is contributed by Amit Katiyar


Javascript


输出:
gksforgks

时间复杂度: O(N),其中 N 是字符串的长度
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live