Home > Article > Backend Development > Modifies a string by replacing all occurrences of a given character with a specified replacement character
In this question we need to replace the characters of a given string based on the given characters in the character pair array. We will discuss two different solutions. In the first method, we replace each character by iterating over the characters and character pairs of the given string.
In the second method, we will use an array of length 26 to store the replacement characters associated with each character and change the characters of the given string.
Problem Statement − We are given a string str containing N lowercase alphabetic characters. Also, we are given an array containing pairs of characters. We need to replace pairs[i][0] characters in the given string with pairs[i][1].
Input – str = "xyz", pairs = {{'x', 'a'}, {'y', 'b'},, {'z', 'c'}}
Output – ‘abc’
Here, 'x' is replaced with 'a', 'y' is replaced with 'b', and 'z' is replaced with 'c'.
Input – str = "abderb", pairs = {{'a', 'e'}, {'b', 't'}, {'e', 'f'}, {'r', 's'}}
Output – ‘etdfst’
In the string, 'a' is replaced with 'e', 'b' is replaced with 't', 'e' is replaced with 'f', and 'r' is replaced with 's'.
In this method, we will iterate over each pair of characters and replace the matching characters in the given string. We need two nested loops to iterate over the string for each loop.
Step 1 - Store the size of the string in variable 'N' and the array in variable 'M'.
Step 2 - Store a copy of the string in the 'temp' variable.
Step 3 - Use a for loop to iterate through the list of pairs.
Step 4 − In the loop, store the first character in variable ‘a’ and the second character in variable ‘b’.
Step 5 - Iterate over the string using nested loops.
Step 6 − In the nested loop, if the current character of the given string is equal to 'a', replace the current character with 'b' in the temporary string.
Step 7 - Return the value of temp.
#include <bits/stdc++.h> using namespace std; string replaceChars(string str, vector<vector<char>> pairs){ // stror the size of the string and the array int N = str.size(), M = pairs.size(); // Create a copy of the string str string temp = str; // Iterate over the array for (int x = 0; x < M; x++){ // store the characters from the pair char a = pairs[x][0], b = pairs[x][1]; // iterate over the string for (int y = 0; y < N; y++){ // If the character is equal to a, then replace it with b if (str[y] == a){ temp[y] = b; } } } return temp; } int main(){ string str = "abderb"; vector<vector<char>> pairs{{'a', 'e'}, {'b', 't'}, {'e', 'f'}, {'r', 's'}}; cout << "The string after replacing with the given characters is - " << replaceChars(str, pairs); return 0; }
The string after replacing with the given characters is - etdfst
Time complexity - O(N*M), where N is the length of the string and M is the length of the character pair array.
Space complexity - O(N), since we store the new string in the temp variable.
In this method, we can create an array of size 26. We can then store the replaceable character at the current character's position. Finally, we can take the replaceable elements from the array and update each character of the string.
Step 1 - Get string size as 'N' and array size as 'M'.
Step 2 - Define "initial" and "final" arrays of length 26.
Step 3 - Loop through the string and store str[Y] in the initial and final array index of "str[Y] - a". Here, str[Y] - 'a' gives an index between 0 and 25 based on the ASCII value of the character.
The reason for storing str[Y] at 'str[Y] - a' position in initial and final array is that if any character is present in the string but not in the character pair, we can Leave it unchanged.
Step 4 - Iterate over the given array of character pairs. Within the loop, use nested loops to iterate over the initial array. If the first character of the current pair is equal to the character of the "initial" array, the characters of the "final" array are updated with the second character of the current pair.
Step 5 − Define the ‘result’ variable and initialize it to an empty string.
Step 6 - Iterate through the input string, get the corresponding character of the current character from the "final" array, and append it to the "result" string.
Step 7 - Return the 'result' string.
#include <bits/stdc++.h> using namespace std; // Function to replace the characters in the string string replaceChars(string str, vector<vector<char>> pairs){ // getting the size of the string and the vector int N = str.size(), M = pairs.size(); // Declare two arrays of size 26 char initial[26]; char final[26]; // Check all existing characters in the string for (int Y = 0; Y < N; Y++){ initial[str[Y] - 'a'] = str[Y]; final[str[Y] - 'a'] = str[Y]; } // Iterate over the range [0, M] for (int X = 0; X < M; X++){ // get characters from the vector char a = pairs[X][0], b = pairs[X][1]; // Iterate over the range [0, 26] for (int Y = 0; Y < 26; Y++){ // If the character is the same as a, then replace it with b in the final array if (initial[Y] == a){ final[Y] = b; } } } string result = ""; // get the final string using the final array for (int Y = 0; Y < N; Y++){ result += final[str[Y] - 'a']; } return result; } int main(){ string str = "aberb"; vector<vector<char>> pairs{{'a', 'e'}, {'b', 't'}, {'e', 'f'}, {'r', 's'}}; cout << "The string after replacing with the given characters is - " << replaceChars(str, pairs); return 0; }
The string after replacing with the given characters is - etfst
Time complexity - O(N), as nested loop, constant iteration only.
Space complexity - O(1), since it uses an array of length 26, which is constant.
The above is the detailed content of Modifies a string by replacing all occurrences of a given character with a specified replacement character. For more information, please follow other related articles on the PHP Chinese website!