给定一个字符串str和两个字符X和Y ,任务是编写一个递归函数,以将所有出现的字符X替换为字符Y。
例子:
Input: str = abacd, X = a, Y = x
Output: xbxcd
Input: str = abacd, X = e, Y = y
Output: abacd
迭代方法:想法是遍历给定的字符串,如果找到任何字符X ,则将该字符替换为Y。
时间复杂度: O(N)
辅助空间: O(1)
递归方法:想法是递归遍历给定的字符串,并将字符X替换为Y。步骤如下:
- 获取字符串str ,字符X和Y。
- 从索引0递归地迭代到字符串长度。
- 基本情况:如果我们到达字符串的末尾,则从函数退出。
if(str[0]=='\0')
return ;
- 递归调用:如果不满足基本要求,则检查第0个索引处的字符是否为X,然后将该字符替换为Y,然后递归地迭代下一个字符。
if(str[0]==X)
str[0] = Y
- 返回语句:在每个递归调用(基本情况除外)中,为下一次迭代返回递归函数。
return recursive_function(str + 1, X, Y)
以下是递归实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to replace all occurrences
// of character c1 with character c2
void replaceCharacter(char input[],
char c1, char c2)
{
// Base Case
// If the string is empty
if (input[0] == '\0') {
return;
}
// If the character at starting
// of the given string is equal
// to c1, replace it with c2
if (input[0] == c1) {
input[0] = c2;
}
// Getting the answer from recursion
// for the smaller problem
return replaceCharacter(input + 1,
c1, c2);
}
// Driver Code
int main()
{
// Given string
char str[] = "abacd";
char c1 = 'a';
char c2 = 'x';
// Function call
replaceCharacter(str, c1, c2);
// Print the string
cout << str;
return 0;
}
Java
// Java program for the above approach
import java.util.*;
import java.io.*;
class GFG{
// Function to replace all occurrences
// of character c1 with character c2
static String replaceCharacter(String str,
char c1, char c2)
{
// Base Case
// If the string is empty
if (str.length() == 1)
{
return str;
}
char x=str.charAt(0);
// If the character at starting
// of the given string is equal
// to c1, replace it with c2
if (str.charAt(0) == c1)
{
x=c2;
str = c2+str.substring(1);
}
// Getting the answer from recursion
// for the smaller problem
return x+replaceCharacter(str.substring(1),
c1, c2);
}
// Driver Code
public static void main(String[] args)
{
// Given string
String str = "abacd";
char c1 = 'a';
char c2 = 'x';
// Function call
System.out.println(replaceCharacter(str, c1, c2));
}
}
// This code is contributed by cyrus18
Python3
# Python3 program for the above approach
# Function to replace all occurrences
# of character c1 with character c2
def replaceCharacter(input, c1, c2):
input = list(str)
# If the character at starting
# of the given string is equal
# to c1, replace it with c2
for i in range(0, len(str)):
if (input[i] == c1):
input[i] = c2;
# Print the string
print(input[i], end = "")
# Driver Code
# Given string
str = "abacd"
c1 = 'a'
c2 = 'x'
# Function call
replaceCharacter(str, c1, c2);
# This code is contributed by sanjoy_62
C#
// C# program for the above approach
using System;
class GFG{
// Function to replace all occurrences
// of character c1 with character c2
static void replaceCharacter(string str,
char c1, char c2)
{
char[] input = str.ToCharArray();
// If the character at starting
// of the given string is equal
// to c1, replace it with c2
for(int i = 0; i < str.Length; i++)
{
if (input[i] == c1)
{
input[i] = c2;
}
// Print the string
Console.Write(input[i]);
}
}
// Driver Code
public static void Main()
{
// Given string
string str = "abacd";
char c1 = 'a';
char c2 = 'x';
// Function call
replaceCharacter(str, c1, c2);
}
}
// This code is contributed by sanjoy_62
输出
xbxcd
时间复杂度: O(N),其中N是字符串的长度
辅助空间: O(1)