您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

通过将给定字符的所有出现替换为指定的替换字符来修改字符串

2024/12/4 10:16:20发布166次查看
在这个问题中,我们需要根据字符对数组中给定的字符替换给定字符串的字符。我们将讨论两种不同的解决方法。在第一种方法中,我们通过遍历给定字符串的字符和字符对来替换每个字符。
在第二种方法中,我们将使用一个长度为26的数组来存储与每个字符相关的替换字符,并改变给定字符串的字符。
问题陈述 − 我们给定了一个包含n个小写字母字符的字符串str。同时,我们给定了包含字符对的数组。我们需要用pairs[i][1]替换给定字符串中的pairs[i][0]字符。
示例例子input – str = xyz, pairs = {{'x', 'a'}, {'y', 'b'},, {'z', 'c'}}
output – ‘abc’
说明在这里,‘x’被替换为‘a’,‘y’被替换为‘b’,‘z’被替换为‘c’。
input – str = abderb, pairs = {{'a', 'e'}, {'b', 't'}, {'e', 'f'}, {'r', 's'}}
output – ‘etdfst’
说明在字符串中,'a'被替换为'e','b'被替换为't','e'被替换为'f','r'被替换为's'。
方法一在这种方法中,我们将迭代每对字符,并在给定的字符串中替换匹配的字符。我们需要两个嵌套循环来迭代每个循环的字符串。
算法步骤 1 - 将字符串的大小存储在变量 'n' 中,并将数组存储在变量 'm' 中。
步骤 2 - 将字符串的副本存储在 'temp' 变量中。
步骤 3 - 使用 for 循环遍历配对列表。
步骤 4 − 在循环中,将第一个字符存储在变量‘a’中,将第二个字符存储在变量‘b’中。
第5步 - 使用嵌套循环迭代字符串。
步骤 6 − 在嵌套循环中,如果给定字符串的当前字符等于 'a',则将当前字符替换为 'b' 在临时字符串中。
第7步 - 返回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
时间复杂度 - o(n*m),其中n是字符串的长度,m是字符对数组的长度。
空间复杂度 - o(n),因为我们将新字符串存储在temp变量中。
方法二在这种方法中,我们可以创建一个大小为26的数组。然后,我们可以将可替换的字符存储在当前字符的位置上。最后,我们可以从数组中取出可替换的元素,并更新字符串的每个字符。
算法步骤 1 - 获取字符串大小为 'n' 和数组大小为 'm'。
第二步 - 定义长度为26的“初始”和“最终”数组。
第三步 - 遍历字符串并将str[y]存储在“str[y] - a”的初始和最终数组索引中。这里,str[y] - 'a'根据字符的ascii值给出0到25之间的索引。
将str[y]存储在初始和最终数组的'str[y] - a'位置的原因是,如果字符串中存在任何字符但在字符对中不存在,我们可以在最终字符串中保持它不变。
第四步 - 迭代给定的字符对数组。在循环中,使用嵌套循环来迭代初始数组。如果当前字符对的第一个字符等于“initial”数组的字符,则使用当前字符对的第二个字符对更新“final”数组的字符。
步骤 5 − 定义‘result’变量,并初始化为空字符串。
步骤 6 - 遍历输入字符串,从“final”数组中获取当前字符的相应字符,并将其追加到“result”字符串中。
步骤 7 - 返回 'result' 字符串。
示例#include <bits/stdc++.h>using namespace std;// function to replace the characters in the stringstring 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
时间复杂度 - o(n),作为嵌套循环,仅进行常量迭代。
空间复杂度 - o(1),因为它使用一个长度为26的数组,是常数。
以上就是通过将给定字符的所有出现替换为指定的替换字符来修改字符串的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product