Given a sentence str. The problem is to remove all the palindromic words from the given sentence.
Examples:
Input : str = "Text contains malayalam and level words"
Output : Text contains and words
Input : str = "abc bcd"
Output : abc bcd
Approach: One by one extract all the words. Check if the current word is not a palindrome then add it to the final string.
Algorithm:
removePalinWords(str, n)
Initialize final_str = "", word = ""
str = str + " "
for i = 0 to n-1
if str[i] != ' ', then
word = word + str[i]
else
if (!(isPalindrome(word)), then
final_str += word + " "
word = ""
return final_str
isPalindrome() function is used to check whether the given string is palindrome or not. Refer this post.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPalindrome(string str) {
int i = 0, j = str.size() - 1;
while (i < j)
if (str[i++] != str[j--])
return false ;
return true ;
}
string removePalinWords(string str) {
string final_str = "" , word = "" ;
str = str + " " ;
int n = str.size();
for ( int i = 0; i < n; i++) {
if (str[i] != ' ' )
word = word + str[i];
else {
if (!(isPalindrome(word)))
final_str += word + " " ;
word = "" ;
}
}
return final_str;
}
int main() {
string str = "Text contains malayalam and level words" ;
cout << removePalinWords(str);
return 0;
}
|
Java
class GFG
{
static boolean isPalindrome(String str)
{
int i = 0 , j = str.length() - 1 ;
while (i < j)
{
if (str.charAt(i++) != str.charAt(j--))
return false ;
}
return true ;
}
static String removePalinWords(String str)
{
String final_str = "" , word = "" ;
str = str + " " ;
int n = str.length();
for ( int i = 0 ; i < n; i++)
{
if (str.charAt(i) != ' ' )
word = word + str.charAt(i);
else
{
if (!(isPalindrome(word)))
final_str += word + " " ;
word = "" ;
}
}
return final_str;
}
public static void main (String[] args)
{
String str = "Text contains malayalam and level words" ;
System.out.print(removePalinWords(str));
}
}
|
Python3
def isPalindrome(string) :
i = 0 ; j = len (string) - 1 ;
while (i < j) :
if (string[i] ! = string[j]) :
return False ;
i + = 1 ;
j - = 1 ;
return True ;
def removePalinWords(string) :
final_str = " "; word = " ";
string = string + " " ;
n = len (string);
for i in range (n) :
if (string[i] ! = ' ' ) :
word = word + string[i];
else :
if ( not (isPalindrome(word))) :
final_str + = word + " " ;
word = "";
return final_str;
if __name__ = = "__main__" :
string = "Text contains malayalam and level words" ;
print (removePalinWords(string));
|
C#
using System;
class GFG {
static bool isPalindrome( string str)
{
int i = 0, j = str.Length - 1;
while (i < j)
{
if (str[i++] != str[j--])
return false ;
}
return true ;
}
static String removePalinWords( string str)
{
string final_str = "" , word = "" ;
str = str + " " ;
int n = str.Length;
for ( int i = 0; i < n; i++)
{
if (str[i] != ' ' )
word = word + str[i];
else
{
if (!(isPalindrome(word)))
final_str += word + " " ;
word = "" ;
}
}
return final_str;
}
public static void Main ()
{
string str = "Text contains malayalam "
+ "and level words" ;
Console.WriteLine(removePalinWords(str));
}
}
|
Javascript
<script>
function isPalindrome(str) {
var i = 0, j = str.length - 1;
while (i < j)
if (str[i++] != str[j--])
return false ;
return true ;
}
function removePalinWords(str) {
var final_str = "" , word = "" ;
str = str + " " ;
var n = str.length;
for ( var i = 0; i < n; i++) {
if (str[i] != ' ' )
word = word + str[i];
else {
if (!(isPalindrome(word)))
final_str += word + " " ;
word = "" ;
}
}
return final_str;
}
var str = "Text contains malayalam and level words" ;
document.write( removePalinWords(str));
</script>
|
OutputText contains and words
Time Complexity: O(N*N), as we are using a loop to traverse N times and checking for palindrome which takes linear time.
Auxiliary Space: O(N), as we are using extra space for final string.
Method 2:Using Built in python functions:
- As all the words in a sentence are separated by spaces.
- We have to split the sentence by spaces using split().
- We split all the words by spaces and store them in a list.
- loop till number of words in list.
- Take a new list and append non palindromic words.
- print newlist.
Implementation:
C++
#include <iostream>
#include <vector>
#include <sstream>
using namespace std;
bool isPalindrome(string word) {
int left = 0;
int right = word.length() - 1;
while (left < right) {
if (word[left] != word[right]) {
return false ;
}
left++;
right--;
}
return true ;
}
vector<string> removePalindrome(string str) {
vector<string> words;
stringstream ss(str);
string word;
while (ss >> word) {
words.push_back(word);
}
vector<string> newList;
for ( auto word : words) {
if (!isPalindrome(word)) {
newList.push_back(word);
}
}
return newList;
}
int main() {
string str = "Text contains malayalam and level words" ;
vector<string> result = removePalindrome(str);
for ( auto word : result) {
cout << word << " " ;
}
cout << endl;
return 0;
}
|
Java
import java.util.ArrayList;
import java.util.List;
public class Main {
public static List<String>
removePalindrome(String string)
{
String[] words = string.split( " " );
List<String> newList = new ArrayList<>();
for (String word : words) {
if (!isPalindrome(word)) {
newList.add(word);
}
}
return newList;
}
public static boolean isPalindrome(String word)
{
int left = 0 ;
int right = word.length() - 1 ;
while (left < right) {
if (word.charAt(left) != word.charAt(right)) {
return false ;
}
left++;
right--;
}
return true ;
}
public static void main(String[] args)
{
String string
= "Text contains malayalam and level words" ;
System.out.println(removePalindrome(string));
}
}
|
Python3
def removePalindrome(string):
lis = list (string.split( " " ))
length = len (lis)
newlis = []
for i in range (length):
if (lis[i] ! = lis[i][:: - 1 ]):
newlis.append(lis[i])
return newlis
string = "Text contains malayalam and level words"
print ( * removePalindrome(string))
|
C#
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
class HelloWorld {
public static bool isPalindrome( string word) {
int left = 0;
int right = word.Length - 1;
while (left < right) {
if (word[left] != word[right]) {
return false ;
}
left++;
right--;
}
return true ;
}
public static List< string > removePalindrome( string str)
{
string [] arr = str.Split( " " );
List< string > newArr = new List< string > ();
for ( int i = 0; i < arr.Length; i++)
{
if (!isPalindrome(arr[i]))
{
newArr.Add(arr[i]);
}
}
return newArr;
}
static void Main() {
string str = "Text contains malayalam and level words" ;
List< string > res = removePalindrome(str);
foreach ( var x in res){
Console.Write(x + " " );
}
}
}
|
Javascript
<script>
function removePalindrome(string)
{
let arr = string.split( " " );
let newArr = [];
for (let i = 0; i < arr.length; i++)
{
if (arr[i] !== arr[i].split( "" ).reverse().join( "" ))
{
newArr.push(arr[i]);
}
}
return newArr;
}
let string = "Text contains malayalam and level words" ;
console.log(removePalindrome(string));
</script>
|
OutputText contains and words
Time Complexity: O(N), as we are using a loop to traverse N times.
Auxiliary Space: O(N), as we are using extra space for newlis.