- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Modify string by replacing all occurrences of given characters by specified replacing characters
In this problem, we need to replace the characters of the given string according to the characters given in the array of pairs of characters. We will discuss two different approaches to solving the problem. In the first approach, we iterate through the characters of the given string and pairs of characters to replace each character.
In the second approach, we will use an array of length 26 to store the replaced character related to each character and change the character of the given string.
Problem statement − We have given a string str containing N lowercase alphabetic characters. Also, we have given the array containing pairs of characters. We need to replace the pairs[i][0] character of a given string with pairs[i][1].
Sample examples
Input – str = "xyz", pairs = {{'x', 'a'}, {'y', 'b'},, {'z', 'c'}}
Output – ‘abc’
Explanation
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’
Explanation
In the string, ‘a’ is replaced with ‘e’, ‘b’ is replaced with ‘t’, ‘e’ is replaced with ‘f’, and ‘r’ is replaced with ‘s’.
Approach 1
In this approach, we will iterate over each pair of characters, and in the given string, we will replace matching characters. We need two nested loops to iterate the string for each loop.
Algorithm
Step 1 − Store the size of the string in the ‘N’ and array in the ‘M’ variable.
Step 2 − Store the copy of the string in the ‘temp’ variable.
Step 3 − Use the for loop to traverse through the list of pairs.
Step 4 − In the loop, store the first character in the ‘a’ and the second character in the ‘b’ variable.
Step 5 − Use a nested loop to iterate through the string.
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 temp string.
Step 7 − Return the value of the temp.
Example
#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; }
Output
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 array of pairs of characters.
Space complexity − O(N), as we store the new string in the temp variable.
Approach 2
In this approach, we can create an array of size 26. After that, we can store replaceable characters at the position of the current character. In the end, we can take replaceable elements from the array and update each character of the string.
Algorithm
Step 1 − Get the string size in ‘N’ and the array in ‘M’.
Step 2 − Define the ‘initial’ and ‘final’ array of length 26.
Step 3 − Traverse the string and store the str[Y] at the ‘str[Y] – a’ an initial and final array index. Here, str[Y] – ‘a’ gives an index between 0 to 25 according to the ASCII value of the character.
Step 4 − Iterate through the given array of pairs of characters. In the loop, use a nested loop to iterate through the initial array. If the first character of the current pair is equal to the ‘initial’ array's character, update the ‘final’ array's character with the current pair's second character pair.
Step 5 − Define the ‘result’ variable, and initialize with the empty string.
Step 6 − Iterate through the input string, get the respective character from the ‘final’ array for the current character, and append it to the ‘result’ string.
Step 7 − return the ‘result’ string.
The reason behind storing the str[Y] at ‘str[Y] – a’ position in the initial and final array is that if any character exists in the string but doesn’t exist in the pair of characters, we can keep it the same in the final string.
Example
#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; }
Output
The string after replacing with the given characters is - etfst
Time complexity − O(N), as a nested loop, makes only constant iterations.
Space complexity − O(1), as it uses an array of length equal to 26, which is constant.