Check if there exists any sub-sequence in a string which is not palindrome
Last Updated :
07 May, 2025
Given a string s consisting of lowercase characters, the task is to check if there exists any subsequence in the string which is not a palindrome. If there is at least 1 such subsequence, then return true, otherwise return false.
Examples:
Input : str = "abaab"
Output: Yes
Explanation: Subsequences "ab" , "abaa" , "aab", are not a palindrome.
Input : str = "zzzz"
Output: NO
Explanation: All possible subsequences are palindrome.
[Naive Approach] Checking Each Subsequence - O(2^n) time and O(n) space
The idea is to generate all possible subsequences of the given string and check each one to see if it's not a palindrome. If we find even one subsequence that is not a palindrome, we return true; otherwise, we return false.
C++
// C++ program to Check if there exists any subsequence
// in a string which is not palindrome
#include <bits/stdc++.h>
using namespace std;
// Function to check if a string is
// palindrome or not.
bool isPalindrome(string s) {
// Empty string is also palindrom
if (s.length() == 0) return true;
int left = 0, right = s.length() - 1;
while (left < right) {
if (s[left] != s[right])
return false;
left++;
right--;
}
return true;
}
bool generateSubsequences(string s, int i, string curr) {
if (i == s.length()) {
return isPalindrome(curr) == false;
}
// Take current character
bool take = generateSubsequences(s, i + 1, curr + s[i]);
// Skip current character
bool noTake = generateSubsequences(s, i + 1, curr);
return take || noTake;
}
bool notPalindromeSub(string s) {
return generateSubsequences(s, 0, "");
}
int main() {
string s = "abaab";
if (notPalindromeSub(s)) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
return 0;
}
Java
// Java program to Check if there exists any subsequence
// in a string which is not palindrome
class GfG {
// Function to check if a string is
// palindrome or not.
static boolean isPalindrome(String s) {
if (s.length() == 0) return true;
int left = 0, right = s.length() - 1;
while (left < right) {
if (s.charAt(left) != s.charAt(right))
return false;
left++;
right--;
}
return true;
}
static boolean generateSubsequences(String s, int i, String curr) {
if (i == s.length()) {
return isPalindrome(curr) == false;
}
// Take current character
boolean take = generateSubsequences(s, i + 1, curr + s.charAt(i));
// Skip current character
boolean noTake = generateSubsequences(s, i + 1, curr);
return take || noTake;
}
static boolean notPalindromeSub(String s) {
return generateSubsequences(s, 0, "");
}
public static void main(String[] args) {
String s = "abaab";
if (notPalindromeSub(s)) {
System.out.println("Yes");
} else {
System.out.println("No");
}
}
}
Python
# Python program to Check if there exists any subsequence
# in a string which is not palindrome
# Function to check if a string is
# palindrome or not.
def isPalindrome(s):
if len(s) == 0:
return True
left = 0
right = len(s) - 1
while left < right:
if s[left] != s[right]:
return False
left += 1
right -= 1
return True
def generateSubsequences(s, i, curr):
if i == len(s):
return isPalindrome(curr) == False
# Take current character
take = generateSubsequences(s, i + 1, curr + s[i])
# Skip current character
noTake = generateSubsequences(s, i + 1, curr)
return take or noTake
def notPalindromeSub(s):
return generateSubsequences(s, 0, "")
if __name__ == "__main__":
s = "abaab"
if notPalindromeSub(s):
print("Yes")
else:
print("No")
C#
// C# program to Check if there exists any subsequence
// in a string which is not palindrome
using System;
class GfG {
// Function to check if a string is
// palindrome or not.
static bool isPalindrome(string s) {
if (s.Length == 0) return true;
int left = 0, right = s.Length - 1;
while (left < right) {
if (s[left] != s[right])
return false;
left++;
right--;
}
return true;
}
static bool generateSubsequences(string s, int i, string curr) {
if (i == s.Length) {
return isPalindrome(curr) == false;
}
// Take current character
bool take = generateSubsequences(s, i + 1, curr + s[i]);
// Skip current character
bool noTake = generateSubsequences(s, i + 1, curr);
return take || noTake;
}
static bool notPalindromeSub(string s) {
return generateSubsequences(s, 0, "");
}
static void Main(string[] args) {
string s = "abaab";
if (notPalindromeSub(s)) {
Console.WriteLine("Yes");
} else {
Console.WriteLine("No");
}
}
}
JavaScript
// JavaScript program to Check if there exists any subsequence
// in a string which is not palindrome
// Function to check if a string is
// palindrome or not.
function isPalindrome(s) {
if (s.length === 0) return true;
let left = 0, right = s.length - 1;
while (left < right) {
if (s[left] !== s[right])
return false;
left++;
right--;
}
return true;
}
function generateSubsequences(s, i, curr) {
if (i === s.length) {
return isPalindrome(curr) === false;
}
// Take current character
let take = generateSubsequences(s, i + 1, curr + s[i]);
// Skip current character
let noTake = generateSubsequences(s, i + 1, curr);
return take || noTake;
}
function notPalindromeSub(s) {
return generateSubsequences(s, 0, "");
}
let s = "abaab";
if (notPalindromeSub(s)) {
console.log("Yes");
} else {
console.log("No");
}
[Expected Approach] Using Hash Set - O(n) time and O(1) space
The idea is to recognize that a subsequence is not a palindrome if and only if the string contains at least two different characters. This is because any subsequence with all same characters is always a palindrome, and any subsequence with at least two different characters can be arranged to form a non-palindrome.
Step by step approach:
- Create a hash set to store unique characters.
- Iterate through each character of the string and add each character to the hash set.
- If the hash set size is >= 2, return true (found a non-palindrome subsequence). Otherwise, return false.
C++
// C++ program to Check if there exists any subsequence
// in a string which is not palindrome
#include <bits/stdc++.h>
using namespace std;
bool notPalindromeSub(string s) {
unordered_set<char> set;
for (char c : s) {
set.insert(c);
}
// If there are at least 2 different characters,
// we can form a non-palindrome subsequence
return set.size() >= 2;
}
int main() {
string s = "abaab";
if (notPalindromeSub(s)) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
return 0;
}
Java
// Java program to Check if there exists any subsequence
// in a string which is not palindrome
import java.util.*;
class GfG {
static boolean notPalindromeSub(String s) {
HashSet<Character> set = new HashSet<>();
for (int i = 0; i < s.length(); i++) {
set.add(s.charAt(i));
}
// If there are at least 2 different characters,
// we can form a non-palindrome subsequence
return set.size() >= 2;
}
public static void main(String[] args) {
String s = "abaab";
if (notPalindromeSub(s)) {
System.out.println("Yes");
} else {
System.out.println("No");
}
}
}
Python
# Python program to Check if there exists any subsequence
# in a string which is not palindrome
def notPalindromeSub(s):
setChars = set()
for c in s:
setChars.add(c)
# If there are at least 2 different characters,
# we can form a non-palindrome subsequence
return len(setChars) >= 2
if __name__ == "__main__":
s = "abaab"
if notPalindromeSub(s):
print("Yes")
else:
print("No")
C#
// C# program to Check if there exists any subsequence
// in a string which is not palindrome
using System;
using System.Collections.Generic;
class GfG {
static bool notPalindromeSub(string s) {
HashSet<char> set = new HashSet<char>();
foreach (char c in s) {
set.Add(c);
}
// If there are at least 2 different characters,
// we can form a non-palindrome subsequence
return set.Count >= 2;
}
static void Main(string[] args) {
string s = "abaab";
if (notPalindromeSub(s)) {
Console.WriteLine("Yes");
} else {
Console.WriteLine("No");
}
}
}
JavaScript
// JavaScript program to Check if there exists any subsequence
// in a string which is not palindrome
function notPalindromeSub(s) {
let set = new Set();
for (let i = 0; i < s.length; i++) {
set.add(s[i]);
}
// If there are at least 2 different characters,
// we can form a non-palindrome subsequence
return set.size >= 2;
}
let s = "abaab";
if (notPalindromeSub(s)) {
console.log("Yes");
} else {
console.log("No");
}
Similar Reads
Check if the characters in a string form a Palindrome in O(1) extra space Given string str. The string may contain lower-case letters, special characters, digits, or even white spaces. The task is to check whether only the letters present in the string are forming a Palindromic combination or not without using any extra space. Note: It is not allowed to use extra space to
10 min read
Check if there exist a string Y such that (X+Y) and (Y+X) are palindromes Given a string X of length N and an integer K, the task is to determine if there exists a string (say Y) of length K such that its concatenation on either side of X makes the resultant string palindrome i.e. X+Y and Y+X both are palindromes. Examples: Input: N = 6, K = 2, X = "abbaab"Output: YesExpl
10 min read
Check if all the palindromic sub-strings are of odd length Given a string 's' check if all of its palindromic sub-strings are of odd length or not. If yes then print "YES" or "NO" otherwise. Examples: Input: str = "geeksforgeeks" Output: NO Since, "ee" is a palindromic sub-string of even length. Input: str = "madamimadam" Output: YES Brute Force Approach: S
10 min read
Check if a linked list of strings forms a palindrome Given a linked list handling string data, check to see whether data is palindrome or not? For example, Input : a -> bc -> d -> dcb -> a -> NULL Output : True String "abcddcba" is palindrome. Output : a -> bc -> d -> ba -> NULL Output : False String "abcdba" is not palindro
6 min read
Check if string can be rearranged so that every Odd length Substring is Palindrome Given a string S. The task is to check whether it is possible to rearrange the string such that every substring of odd length is a palindrome. Examples: Input: S = "oiooi" Output: YES The string can be rearranged as "oioio" Input: S = "yuyuo" Output: NO Approach: The very first observation is if all
7 min read
Queries to check if substring[L...R] is palindrome or not Given a string str and Q queries. Every query consists of two numbers L and R. The task is to print if the sub-string[L...R] is palindrome or not. Examples: Input: str = "abacccde", Q[][] = {{0, 2}, {1, 2}, {2, 4}, {3, 5}} Output: Yes No No Yes Input: str = "abaaab", Q[][] = {{0, 1}, {1, 5}} Output:
12 min read