Given a string S representing a sentence, the task is to reverse the order of all palindromic words present in the sentence.
Examples:
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.
Approach: Follow the steps below to solve the problem:
Below is the implementation of the above approach:
Java
import java.util.*;
class GFG{
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 ;
}
static void printReverse(String sentence)
{
ArrayList<String> newlist = new ArrayList<String>();
ArrayList<String> lis = new ArrayList<String>();
String temp = "" ;
for ( char x: sentence.toCharArray())
{
if (x == ' ' )
{
lis.add(temp);
temp = "" ;
}
else
temp += x;
}
lis.add(temp);
for (String x: lis)
{
if (palindrome(x))
newlist.add(x);
}
Collections.reverse(newlist);
int j = 0 ;
for ( int i = 0 ; i < lis.size(); i++)
{
if (palindrome(lis.get(i)))
{
lis.set(i,newlist.get(j));
j = j + 1 ;
}
}
for (String x : lis)
System.out.print(x + " " );
}
public static void main(String[] args)
{
String sentence = "mom and dad went to eye hospital" ;
printReverse(sentence);
}
}
|
Python3
def palindrome(string):
if (string = = string[:: - 1 ]):
return True
else :
return False
def printReverse(sentence):
newlist = []
lis = list (sentence.split())
for i in lis:
if (palindrome(i)):
newlist.append(i)
newlist.reverse()
j = 0
for i in range ( len (lis)):
if (palindrome(lis[i])):
lis[i] = newlist[j]
j = j + 1
for i in lis:
print (i, end = " " )
sentence = "mom and dad went to eye hospital"
printReverse(sentence)
|
C#
using System;
using System.Collections.Generic;
class GFG{
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 ;
}
static void printReverse( string sentence)
{
List< string > newlist = new List< string >();
List< string > lis = new List< string >();
string temp = "" ;
foreach ( char x in sentence)
{
if (x == ' ' )
{
lis.Add(temp);
temp = "" ;
}
else
temp += x;
}
lis.Add(temp);
foreach ( string x in lis)
{
if (palindrome(x))
newlist.Add(x);
}
newlist.Reverse();
int j = 0;
for ( int i = 0; i < lis.Count; i++)
{
if (palindrome(lis[i]))
{
lis[i] = newlist[j];
j = j + 1;
}
}
foreach ( string x in lis)
Console.Write(x + " " );
}
public static void Main()
{
string sentence = "mom and dad went to eye hospital" ;
printReverse(sentence);
}
}
|
Javascript
<script>
function palindrome(str)
{
var st = 0;
var ed = str.length - 1;
while (st < ed)
{
if (str[st] == str[ed])
{
st++;
ed--;
}
else
return false ;
}
return true ;
}
function printReverse(sentence)
{
var newlist = [];
var lis = [];
var temp = "" ;
for ( var i =0; i<sentence.length;i++)
{
if (sentence[i] == ' ' )
{
lis.push(temp);
temp = "" ;
}
else
temp += sentence[i];
}
lis.push(temp);
for ( var i =0; i<lis.length;i++)
{
if (palindrome(lis[i]))
newlist.push(lis[i]);
}
newlist.reverse();
var j = 0;
for ( var i = 0; i < lis.length; i++)
{
if (palindrome(lis[i]))
{
lis[i] = newlist[j];
j = j + 1;
}
}
for ( var i = 0; i < lis.length; i++)
{
document.write( lis[i] + " " );
}
}
var sentence = "mom and dad went to eye hospital" ;
printReverse(sentence);
</script>
|
C++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
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 ;
}
void printReverse(string sentence)
{
vector<string> newlist;
vector<string> lis;
string temp = "" ;
for ( char x: sentence)
{
if (x == ' ' )
{
lis.push_back(temp);
temp = "" ;
}
else
temp += x;
}
lis.push_back(temp);
for (string x: lis)
{
if (palindrome(x))
newlist.push_back(x);
}
reverse(newlist.begin(), newlist.end());
int j = 0;
for ( int i = 0; i < lis.size(); i++)
{
if (palindrome(lis[i]))
{
lis[i] = newlist[j];
j = j + 1;
}
}
for (string x : lis)
cout << x << " " ;
}
int main()
{
string sentence = "mom and dad went to eye hospital" ;
printReverse(sentence);
return 0;
}
|
Output: eye and dad went to mom hospital
Time Complexity: O(N)
Auxiliary Space: O(N) because it is using extra space for list newlist and lis