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.
- 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
- Check if any anagram of a string is palindrome or not
- Check if a string is palindrome in C using pointers
- Check if it is possible to create a palindrome string from given N
- C Program to Check if a Given String is Palindrome
- Check if a given string is a rotation of a palindrome
- Given two strings check which string makes a palindrome first
- Python program to check if a string is palindrome or not
- Recursive function to check if a string is palindrome
- Check if characters of a given string can be rearranged to form a palindrome
- Check if a string can be rearranged to form special palindrome
- Python program to check if given string is vowel Palindrome
- Check if the characters in a string form a Palindrome in O(1) extra space
- Longest palindrome subsequence with O(n) space
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.