Remove all the palindromic words from the given sentence

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 : This 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.

C++

// C++ implementation to remove all the 
// palindromic words from the given sentence
#include <bits/stdc++.h>
using namespace std;

// function to check if 'str' is palindrome
bool isPalindrome(string str) {
  int i = 0, j = str.size() - 1;

  // traversing from both the ends
  while (i < j)

    // not palindrome
    if (str[i++] != str[j--])
      return false;

  // palindrome
  return true;
}

// function to remove all the palindromic words
// from the given sentence
string removePalinWords(string str) {

  // 'final_str' to store the final string and
  // 'word' to one by one store each word of 'str'
  string final_str = "", word = "";

  // add space at the end of 'str'
  str = str + " ";
  int n = str.size();

  // traversing 'str'
  for (int i = 0; i < n; i++) {

    // accumulating characters of the current word
    if (str[i] != ' ')
      word = word + str[i];

    else {

      // if 'word' is not palindrome then a
      // add it to 'final_str'
      if (!(isPalindrome(word)))
        final_str += word + " ";

      // reset
      word = "";
    }
  }

  // required final string
  return final_str;
}

// Driver program to test above
int main() {
  string str = "Text contains malayalam and level words";
  cout << removePalinWords(str);
  return 0;
}

Java

// Java implementation to remove all the 
// palindromic words from the given sentence

class GFG
{
    // function to check if 'str' is palindrome
    static boolean isPalindrome(String str) 
    {
        int i = 0, j = str.length() - 1;
        
        // traversing from both the ends
        while (i < j)
        {
            // not palindrome
            if (str.charAt(i++) != str.charAt(j--))
            return false;
        }
        // palindrome
        return true;
    }
    
    // function to remove all the palindromic words
    // from the given sentence
    static String removePalinWords(String str) 
    {
    
        // 'final_str' to store the final string and
        // 'word' to one by one store each word of 'str'
        String final_str = "", word = "";
        
        // add space at the end of 'str'
        str = str + " ";
        int n = str.length();
        
        // traversing 'str'
        for (int i = 0; i < n; i++) 
        {
        
            // accumulating characters of the current word
            if (str.charAt(i) != ' ')
            word = word + str.charAt(i);
        
            else 
            {
        
                // if 'word' is not palindrome then a
                // add it to 'final_str'
                if (!(isPalindrome(word)))
                    final_str += word + " ";
            
                // reset
                word = "";
            }
        }
        
        // required final string
        return final_str;
    }
    
    // Driver code
    public static void main (String[] args)
    {
        String str = "Text contains malayalam and level words";
    System.out.print(removePalinWords(str));
    }
}

// This code is contributed by Anant Agarwal.

C#

// C# implementation to remove all the 
// palindromic words from the given sentence
using System;

class GFG {
    
    // function to check if 'str' is 
    // palindrome
    static bool isPalindrome(string str) 
    {
        int i = 0, j = str.Length - 1;
        
        // traversing from both the ends
        while (i < j)
        {
            
            // not palindrome
            if (str[i++] != str[j--])
            return false;
        }
        
        // palindrome
        return true;
    }
    
    // function to remove all the 
    // palindromic words from the 
    // given sentence
    static String removePalinWords(string str) 
    {
    
        // 'final_str' to store the final 
        // string and 'word' to one by one
        // store each word of 'str'
        string final_str = "", word = "";
        
        // add space at the end of 'str'
        str = str + " ";
        int n = str.Length;
        
        // traversing 'str'
        for (int i = 0; i < n; i++) 
        {
        
            // accumulating characters of 
            // the current word
            if (str[i] != ' ')
                word = word + str[i];
            else
            {
        
                // if 'word' is not palindrome
                // then a add it to 'final_str'
                if (!(isPalindrome(word)))
                    final_str += word + " ";
        
                // reset
                word = "";
            }
        }
        
        // required final string
        return final_str;
    }
    
    // Driver code
    public static void Main ()
    {
        string str = "Text contains malayalam "
                           + "and level words";
        Console.WriteLine(removePalinWords(str));
    }
}

// This code is contributed by vt_m.

Output:

Text contains and words

Time Complexity: O(n).





Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



1 Average Difficulty : 1/5.0
Based on 3 vote(s)