Skip to content
Related Articles

Related Articles

Check if there exists any sub-sequence in a string which is not palindrome

View Discussion
Improve Article
Save Article
  • Last Updated : 10 Aug, 2021
View Discussion
Improve Article
Save Article

Given a string of lowercase English alphabets. The task is to check if there exists any subsequence in the string which is not a palindrome. If there is at least 1 subsequence that is not a palindrome then print YES, otherwise print NO.
Examples
 

Input : str = "abaab"
Output : YES
Subsequences "ab" or "abaa" or "aab", are not palindrome.

Input : str = "zzzz"
Output : NO
All possible subsequences are palindrome.

 

The main observation is that if the string contains at least two distinct characters, then there will always be a subsequence of length at least two which is not a palindrome. Only if all the characters of the string are the same then there will not be any subsequence that is not a palindrome. Because in an optimal way we can choose any two distinct characters from a string and place them in same order one after each to form a non-palindromic string.
Below is the implementation of above approach: 
 

C++




// C++ program to check if there exists
// at least 1 sub-sequence in a string
// which is not palindrome
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if there exists
// at least 1 sub-sequence in a string
// which is not palindrome
bool isAnyNotPalindrome(string s)
{
    // use set to count number of
    // distinct characters
    set<char> unique;
 
    // insert each character in set
    for (int i = 0; i < s.length(); i++)
        unique.insert(s[i]);
 
    // If there is more than 1 unique
    // characters, return true
    if (unique.size() > 1)
        return true;
    // Else, return false
    else
        return false;
}
 
// Driver code
int main()
{
    string s = "aaaaab";
 
    if (isAnyNotPalindrome(s))
        cout << "YES";
    else
        cout << "NO";
 
    return 0;
}

Java




// Java program to check if there exists
// at least 1 sub-sequence in a string
// which is not palindrome
 
import java.util.*;
class GFG
{
     
    // Function to check if there exists
    // at least 1 sub-sequence in a string
    // which is not palindrome
    static boolean isAnyNotPalindrome(String s)
    {
        // use set to count number of
        // distinct characters
        Set<Character> unique=new HashSet<Character>();
     
        // insert each character in set
        for (int i = 0; i < s.length(); i++)
            unique.add(s.charAt(i));
     
        // If there is more than 1 unique
        // characters, return true
        if (unique.size() > 1)
            return true;
        // Else, return false
        else
            return false;
    }
     
    // Driver code
    public static void main(String []args)
    {
        String s = "aaaaab";
     
        if (isAnyNotPalindrome(s))
            System.out.println("YES");
        else
            System.out.println("NO");
     
    }
}

Python3




# Python3 program to check if there exists
# at least 1 sub-sequence in a string
# which is not palindrome
 
 
# Function to check if there exists
# at least 1 sub-sequence in a string
# which is not palindrome
def isAnyNotPalindrome(s):
 
    # use set to count number of
    # distinct characters
    unique=set()
 
    # insert each character in set
    for i in range(0,len(s)):
        unique.add(s[i])
 
    # If there is more than 1 unique
    # characters, return true
    if (len(unique) > 1):
        return True
         
    # Else, return false
    else:
        return False
 
 
# Driver code
if __name__=='__main__':
    s = "aaaaab"
 
    if (isAnyNotPalindrome(s)):
        print("YES")
    else:
        print("NO")
 
# This code is contributed by
# ihritik

C#




// C# program to check if there exists
// at least 1 sub-sequence in a string
// which is not palindrome
using System;
using System.Collections.Generic;
 
class GFG
{
     
    // Function to check if there exists
    // at least 1 sub-sequence in a string
    // which is not palindrome
    static bool isAnyNotPalindrome(String s)
    {
        // use set to count number of
        // distinct characters
        HashSet<char> unique=new HashSet<char>();
     
        // insert each character in set
        for (int i = 0; i < s.Length; i++)
            unique.Add(s[i]);
     
        // If there is more than 1 unique
        // characters, return true
        if (unique.Count > 1)
            return true;
        // Else, return false
        else
            return false;
    }
     
    // Driver code
    public static void Main(String []args)
    {
        String s = "aaaaab";
     
        if (isAnyNotPalindrome(s))
            Console.WriteLine("YES");
        else
            Console.WriteLine("NO");
    }
}
 
// This code contributed by Rajput-Ji

Javascript




<script>
 
// JavaScript program to check if there exists
// at least 1 sub-sequence in a string
// which is not palindrome
 
// Function to check if there exists
// at least 1 sub-sequence in a string
// which is not palindrome
function isAnyNotPalindrome(s)
{
    // use set to count number of
    // distinct characters
    var unique = new Set();
 
    // insert each character in set
    for (var i = 0; i < s.length; i++)
        unique.add(s[i]);
 
    // If there is more than 1 unique
    // characters, return true
    if (unique.size > 1)
        return true;
    // Else, return false
    else
        return false;
}
 
// Driver code
var s = "aaaaab";
if (isAnyNotPalindrome(s))
    document.write( "YES");
else
    document.write( "NO");
 
 
</script>

Output: 

YES

 

Time Complexity: O(N * logN), where N is the length of the string.
Auxiliary Space: O(N)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!