通过反转所有回文单词的出现顺序来修改句子


问题陈述

给定一个包含 N 个单词的字符串 str。我们需要找到给定字符串中的所有回文单词,并通过反转所有回文单词的顺序来创建一个新字符串。

示例

输入

str = ‘nayan was gone to navjivan eye hospital’

输出

‘eye was gone to navjivan nayan hospital’

解释

该字符串包含三个回文单词:nayan、navjivan 和 eye。我们反转了所有三个单词的顺序,并将所有其他单词保持不变。

输入

‘Hello, users! How are you?’

输出

‘Hello, users! How are you?’

解释

它给出的输出与字符串不包含任何回文单词的情况相同。

输入

‘Your eye is beautiful.’

输出

‘Your eye is beautiful.’

解释

它给出的输出与字符串只包含单个回文单词的情况相同。

方法 1

在这种方法中,我们首先将字符串分割成单词。之后,我们将过滤所有回文单词。接下来,我们反转所有回文单词的顺序。

最后,我们遍历字符串,如果当前单词是回文,我们将用反向顺序的另一个回文单词替换它。

算法

  • 步骤 1 - 通过传递字符串作为参数来执行 reversePlaindromic() 函数,该函数返回结果字符串。

  • 步骤 2 - 创建 isPalindrome() 函数,该函数检查单词是否为回文。

  • 步骤 2.1 - 将 'start' 初始化为 0,将 'end' 初始化为字符串长度 - 1。

  • 步骤 2.2 - 使用 while 循环遍历字符串,并比较第一个和最后一个字符,比较第二个和倒数第二个字符,依此类推。如果任何字符不匹配,则返回 false,因为它不是回文字符串。

  • 步骤 2.3 - 如果字符串是回文,则返回 true。

  • 步骤 3 - 创建一个向量来存储字符串的单词。此外,定义 'temp' 变量来存储单词。

  • 步骤 4 - 使用 for 循环遍历字符串,如果字符不等于空格 (' '),则将字符附加到 temp。否则,将 temp 的值推送到 allWords 向量。

  • 步骤 5 - 遍历 allWords 向量并使用 isPalindrome() 函数检查当前单词是否为回文。如果是,则将单词推送到 'palindromWords' 向量。

  • 步骤 6 - 反转 'palindromWords' 列表。

  • 步骤 7 - 现在,再次遍历 'allWords' 向量,并检查当前单词是否为回文。如果是,则将其替换为 'palindromWords' 列表中相应的单词。

  • 步骤 8 - 遍历 'palindromWords' 列表,并通过将所有单词附加到 result 变量来创建一个字符串。返回结果字符串。

示例

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// Function to check if a string is a palindrome
bool isPalindrome(string str){
   int start = 0;
   int end = str.length() - 1;
   // iterate till start < end
   while (start < end){
      // check if the character at the start and end are not the same and return false, else increment start and decrement end
      if (str[start] != str[end]){
         return false;
      } else {
         start++;
         end--;
      }
   }
   return true;
}
string reversePalindromic(string str) {
   // vectors to store all words and palindromic words
   vector<string> palindromWords;
   vector<string> allWords;
   // variable to store single word
   string temp = "";
   for (char x : str) {
      // If the current character is not space, then append it to temp; else, add temp to palindrome words and make temp NULL
      if (x != ' ') {
         temp += x;
      } else {
         allWords.push_back(temp);
         temp = "";
      }
   }
   // push the last word to all words
   allWords.push_back(temp);
   // fetch all palindromic words
   for (string x : allWords){
      if (isPalindrome(x)){
         // Update newlist
         palindromWords.push_back(x);
      }
   }
   // Reverse the vector
   reverse(palindromWords.begin(), palindromWords.end());
   int k = 0;
   for (int i = 0; i < allWords.size(); i++){
      // If the current word is a palindrome, push it to palindrome words
      if (isPalindrome(allWords[i])){
         allWords[i] = palindromWords[k];
         k++;
      }
   }
   string result = "";
   for (string x : allWords) {
      result += x;
      result += " ";
   }
   return result;
}
int main(){
   string str = "nayan was gone to navjivan eye hospital";
   string reverse = reversePalindromic(str);
   cout << reverse << endl;
   return 0;
}

输出

eye was gone to navjivan nayan hospital
  • 时间复杂度 - O(N),因为我们正在遍历长度为 N 的字符串。

  • 空间复杂度 - O(K),因为我们使用列表来存储单词,其中 k 是字符串中单词的总数。

结论

我们学习了如何从句子中获取所有回文单词并将它们以相反的顺序添加。在上面的代码中,编码人员可以尝试更改 isPalindrome() 函数的实现以学习新知识。

更新于:2023年7月18日

117 次查看

开启你的职业生涯

完成课程获得认证

开始
广告
© . All rights reserved.