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.
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:
Time Complexity: O(N), where N is the length of the string.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Check whether a subsequence exists with sum equal to k if arr[i]> 2*arr[i-1]
- Check if a subsequence of length K with odd sum exists
- Queries to check if string B exists as substring in string A
- Check if a given string is Even-Odd Palindrome or not
- C Program to Check if a Given String is Palindrome
- Check given string is oddly palindrome or not
- Check if a string is palindrome in C using pointers
- Check given string is oddly palindrome or not | Set 2
- Check if any anagram of a string is palindrome or not
- Check if it is possible to create a palindrome string from given N
- Check if a given string is a rotation of a palindrome
- Check if both halves of a string are Palindrome or not
- Check whether the given string is Palindrome using Stack
- Python program to check if a string is palindrome or not
- Recursive function to check if a string is palindrome
- Java program to check whether a string is a Palindrome
- Given two strings check which string makes a palindrome first
- Check if there exists a permutation of given string which doesn't contain any monotonous substring
- Check if characters of a given string can be rearranged to form a palindrome
- Check if string is palindrome after removing all consecutive duplicates
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.