给定字符串str ,任务是编写一个递归程序以删除字符串中所有出现的字符X。
例子:
Input: str = “geeksforgeeks”, c = ‘e’
Output: gksforgks
Input: str = “geeksforgeeks”, c = ‘g’
Output: eeksforeeks
迭代方法:此问题的迭代方法可以在本文中找到。
递归方法:以下是步骤:
- 获取字符串str和字符X为这字符X将被移除。
- 递归地迭代字符串中的所有字符:
- 基本情况:如果递归调用的字符串str的长度为0,则从函数返回空字符串。
if(str.length()==0) { return ""; }
- 递归调用:如果不满足基本条件,则检查索引为0的字符是否为X,然后递归迭代子字符串以删除第一个字符。
if (str[0] == X) { return recursive_function(str.substr(1), X); }
- 返回语句:在每次递归调用时(基本情况和上述条件除外),都为下一次迭代返回递归函数,包括第0个index处的字符。
return str[0] + recursive_function(str.substr(1), X)
- 基本情况:如果递归调用的字符串str的长度为0,则从函数返回空字符串。
下面是上述方法的实现:
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
输出:
gksforgks
时间复杂度: O(N),其中N是字符串的长度
辅助空间: O(1)