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");
}