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

10 Aug, 2021
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++
Java
Python3
C#
Javascript
// 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 palindromebool 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 codeint main(){    string s = "aaaaab";     if (isAnyNotPalindrome(s))        cout << "YES";    else        cout << "NO";     return 0;}
// 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 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 palindromedef 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 codeif __name__=='__main__':    s = "aaaaab"     if (isAnyNotPalindrome(s)):        print("YES")    else:        print("NO") # This code is contributed by# ihritik
// C# program to check if there exists// at least 1 sub-sequence in a string// which is not palindromeusing 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
<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 palindromefunction 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 codevar s = "aaaaab";if (isAnyNotPalindrome(s))    document.write( "YES");else    document.write( "NO");  </script>
YES
 
Time Complexity: O(N * logN), where N is the length of the string.Auxiliary Space: O(N)
ihritik
Rajput-Ji
rutvik_56
pankajsharmagfg
palindrome
subsequence
Data Structures
Strings
Data Structures
Strings
palindrome
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Introduction to Tree Data Structure
Program to implement Singly Linked List in C++ using class
Hash Functions and list/types of Hash functions
Insertion in a B+ tree
Shortest path in a directed graph by Dijkstra’s algorithm
Write a program to reverse an array or string
Reverse a string in Java
Longest Common Subsequence | DP-4
Write a program to print all permutations of a given string
C++ Data Types