给定一个表示句子的字符串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 next wow.
处理方法:按照以下步骤解决问题:
- 迭代字符串S的字符并使用 split() 从句子中拆分空格分隔的单词并将它们存储在列表中,例如lis 。
- 使用 append() 将所有回文单词附加到一个新列表中,比如newlist 函数。
- 使用 reverse()函数反转这个新列表。
- 遍历列表lis并继续用newlist[i] (最初, i=0 )替换回文单词并将i增加1 。
- 打印修改后的句子
下面是上述方法的实现:
C++
Java
// Java implementation of
// the above approach
import java.util.*;
class GFG{
// Function to check if a
// string S is a palindrome
static boolean palindrome(String str)
{
int st = 0;
int ed = str.length() - 1;
while (st < ed)
{
if (str.charAt(st) == str.charAt(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
ArrayList newlist = new ArrayList();
ArrayList lis = new ArrayList();
// Stores the words in the list
String temp = "";
for(char x: sentence.toCharArray())
{
if (x == ' ')
{
lis.add(temp);
temp = "";
}
else
temp += x;
}
lis.add(temp);
// Traversing the list
for(String x: lis)
{
// If current word is a palindrome
if (palindrome(x))
// Update newlist
newlist.add(x);
}
// Reverse the newlist
Collections.reverse(newlist);
int j = 0;
// Traverse the list
for(int i = 0; i < lis.size(); i++)
{
// If current word is a palindrome
if (palindrome(lis.get(i)))
{
// Update lis[i]
lis.set(i,newlist.get(j));
// Increment j
j = j + 1;
}
}
// Print the updated sentence
for(String x : lis)
System.out.print(x + " ");
}
// Driver code
public static void main(String[] args)
{
String sentence = "mom and dad went to eye hospital";
printReverse(sentence);
}
}
// This code is contributed by offbeat
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
Javascript
输出:
eye and dad went to mom hospital
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live