Check if there exists any sub-sequence in a string which is not palindrome
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 which 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 same then there will not be any subsequence which 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 |
YES
Time Complexity: O(N), where N is the length of the string.
Recommended Posts:
- Check whether a subsequence exists with sum equal to k if arr[i]> 2*arr[i-1]
- Queries to check if string B exists as substring in string A
- C Program to Check if a Given String is Palindrome
- Check if it is possible to create a palindrome string from given N
- Check whether the given string is Palindrome using Stack
- Check if any anagram of a string is palindrome or not
- Check if a string is palindrome in C using pointers
- Check if a given string is a rotation of a palindrome
- Recursive function to check if a string is palindrome
- Java program to check whether a string is a Palindrome
- Python program to check if a string is palindrome or not
- Given two strings check which string makes a palindrome first
- Check if characters of a given string can be rearranged to form a palindrome
- Python program to check if given string is vowel Palindrome
- Check if a string can be rearranged to form special palindrome
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


