修改字串,將所有給定字元替換為指定的替換字元
在這個問題中,我們需要根據字元對陣列中給定的字元對來替換給定字串中的字元。我們將討論兩種不同的解決方法。在第一種方法中,我們迭代給定字串的字元和字元對,以替換每個字元。
在第二種方法中,我們將使用長度為26的陣列來儲存與每個字元相關的替換字元,並更改給定字串的字元。
問題陳述 − 我們給定一個包含N個小寫字母字元的字串str。此外,我們還給定一個包含字元對的陣列。我們需要將給定字串的pairs[i][0]字元替換為pairs[i][1]。
示例
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”,則在temp字串中將當前字元替換為“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”。
步驟2 − 定義長度為26的“initial”和“final”陣列。
步驟3 − 遍歷字串並將str[Y]儲存在“str[Y] – a”的“initial”和“final”陣列索引處。這裡,str[Y] – ‘a’根據字元的ASCII值給出0到25之間的索引。
步驟4 − 迭代給定的字元對陣列。在迴圈中,使用巢狀迴圈迭代“initial”陣列。如果當前對的第一個字元等於“initial”陣列的字元,則使用當前對的第二個字元對更新“final”陣列的字元。
步驟5 − 定義“result”變數,並用空字串初始化。
步驟6 − 迭代輸入字串,從“final”陣列中獲取當前字元的相應字元,並將其附加到“result”字串。
步驟7 − 返回“result”字串。
將str[Y]儲存在“str[Y] – a”位置的原因是,如果字串中存在任何字元但在字元對中不存在,我們可以在最終字串中保持不變。
示例
#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
時間複雜度 − O(N),因為巢狀迴圈只進行恆定次數的迭代。
空間複雜度 − O(1),因為它使用長度等於26的陣列,這是一個常數。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP