给定表示句子的字符串S ,任务是颠倒句子中所有回文词的顺序。
例子:
Input: S = “mom and dad went to eye hospital”
Output: eye and dad went to mom hospital
Explanation: All palindromic words present in the string are “mom”, “dad” and “eye“, in the order of their occurrence. Reversing the order of their occurrence generates the sequence {“eye”, “dad”, “mom”}. Therefore, the modified string is “eye and dad went to mom hospital”.
Input : S = “wow it is next level”
Output: level it is wow.
方法:请按照以下步骤解决问题:
- 迭代字符串S的字符,并使用split()将句子中用空格分隔的单词分开,然后将它们存储在列表中,例如lis 。
- 使用append()将所有回文词追加到一个新列表,例如newlist 函数。
- 使用reverse()函数反转此新列表。
- 遍历列表LIS和保持与newlist [I](最初,I = 0)和增量i增加1替换回文字。
- 打印修改后的句子
下面是上述方法的实现:
C++
// C++ implementation of
// the above approach
#include
using namespace std;
// Function to check if a
// string S is a palindrome
bool palindrome(string str)
{
int st = 0;
int ed = str.length() - 1;
while (st < ed)
{
if (str[st] == str[ed])
{
st++;
ed--;
}
else
return false;
}
return true;
}
// Function to print the modified string
// after reversing teh order of occurrences
// of all palindromic words in the sentence
void printReverse(string sentence)
{
// Stores the palindromic words
vector newlist;
vector lis;
// Stores the words in the list
string temp = "";
for(auto x : sentence)
{
if (x == ' ')
{
lis.push_back(temp);
temp = "";
}
else
temp += x;
}
lis.push_back(temp);
// Traversing the list
for(auto x: lis)
{
// If current word is a palindrome
if (palindrome(x))
// Update newlist
newlist.push_back(x);
}
// Reverse the newlist
reverse(newlist.begin(),newlist.end());
int j = 0;
// Traverse the list
for(int i = 0; i < lis.size(); i++)
{
// If current word is a palindrome
if (palindrome(lis[i]))
{
// Update lis[i]
lis[i] = newlist[j];
// Increment j
j = j + 1;
}
}
// Print the updated sentence
for(auto x: lis)
cout << x << " ";
}
// Driver Code
int main()
{
string sentence = "mom and dad went to eye hospital";
printReverse(sentence);
}
// This code is contributed by bgangwar59
Python3
# Python implementation of
# the above approach
# Function to check if a
# string S is a palindrome
def palindrome(string):
if(string == string[::-1]):
return True
else:
return False
# Function to print the modified string
# after reversing teh order of occurrences
# of all palindromic words in the sentence
def printReverse(sentence):
# Stores the palindromic words
newlist = []
# Stores the words in the list
lis = list(sentence.split())
# Traversing the list
for i in lis:
# If current word is a palindrome
if(palindrome(i)):
# Update newlist
newlist.append(i)
# Reverse the newlist
newlist.reverse()
j = 0
# Traverse the list
for i in range(len(lis)):
# If current word is a palindrome
if(palindrome(lis[i])):
# Update lis[i]
lis[i] = newlist[j]
# Increment j
j = j + 1
# Print the updated sentence
for i in lis:
print(i, end =" ")
# Driver Code
sentence = "mom and dad went to eye hospital"
printReverse(sentence)
C#
// C# implementation of
// the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to check if a
// string S is a palindrome
static bool palindrome(string str)
{
int st = 0;
int ed = str.Length - 1;
while (st < ed)
{
if (str[st] == str[ed])
{
st++;
ed--;
}
else
return false;
}
return true;
}
// Function to print the modified string
// after reversing teh order of occurrences
// of all palindromic words in the sentence
static void printReverse(string sentence)
{
// Stores the palindromic words
List newlist = new List();
List lis = new List();
// Stores the words in the list
string temp = "";
foreach(char x in sentence)
{
if (x == ' ')
{
lis.Add(temp);
temp = "";
}
else
temp += x;
}
lis.Add(temp);
// Traversing the list
foreach(string x in lis)
{
// If current word is a palindrome
if (palindrome(x))
// Update newlist
newlist.Add(x);
}
// Reverse the newlist
newlist.Reverse();
int j = 0;
// Traverse the list
for(int i = 0; i < lis.Count; i++)
{
// If current word is a palindrome
if (palindrome(lis[i]))
{
// Update lis[i]
lis[i] = newlist[j];
// Increment j
j = j + 1;
}
}
// Print the updated sentence
foreach(string x in lis)
Console.Write(x + " ");
}
// Driver Code
public static void Main()
{
string sentence = "mom and dad went to eye hospital";
printReverse(sentence);
}
}
// This code is contributed by ukasp
输出:
eye and dad went to mom hospital
时间复杂度: O(N)
辅助空间: O(1)