首頁 > 後端開發 > C++ > 主體

透過將給定字元的所有出現替換為指定的替換字元來修改字串

WBOY
發布: 2023-09-08 10:17:02
轉載
824 人瀏覽過

透過將給定字元的所有出現替換為指定的替換字元來修改字串

在這個問題中,我們需要根據字元對陣列中給定的字元替換給定字串的字元。我們將討論兩種不同的解決方法。在第一種方法中,我們透過遍歷給定字串的字元和字元對來替換每個字元。

在第二種方法中,我們將使用一個長度為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 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的數組,是常數。

以上是透過將給定字元的所有出現替換為指定的替換字元來修改字串的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!