Check if a string can be split into even length palindromic substrings
Last Updated :
24 May, 2022
Given a string str, the task is to check if it is possible to split the given string into even length palindromic substrings.
Examples:
Input: str = "abbacc"
Output: Yes
Explanation:
Strings "abba" and "cc" are the even length palindromic substrings.
Input: str = "abcde"
Output: No
Explanation:
No even length palindromic substrings possible.
Approach: The idea is to use Stack Data Structure. Below are the steps:
- Initialise an empty stack.
- Traverse the given string str.
- For each character in the given string, do the following:
- If the character is equal to the character at the top of the stack then pop the top element from the stack.
- Else push the current character into the stack.
- If stack is empty after the above steps then the given string can be break into a palindromic substring of even length.
- Else the given string cannot be break into palindromic substring of even length.
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to check string str can be
// split a string into even length
// palindromic substrings
bool check(string s, int n)
{
// Initialize a stack
stack<char> st;
// Iterate the string
for (int i = 0; i < n; i++) {
// If the i-th character is same
// as that at the top of the stack
// then pop the top element
if (!st.empty() && st.top() == s[i])
st.pop();
// Else push the current character
// into the stack
else
st.push(s[i]);
}
// If the stack is empty, then even
// palindromic substrings are possible
if (st.empty()) {
return true;
}
// Else not-possible
else {
return false;
}
}
// Driver Code
int main()
{
// Given string
string str = "aanncddc";
int n = str.length();
// Function Call
if (check(str, n)) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to check String str can be
// split a String into even length
// palindromic subStrings
static boolean check(String s, int n)
{
// Initialize a stack
Stack<Character> st = new Stack<Character>();
// Iterate the String
for(int i = 0; i < n; i++)
{
// If the i-th character is same
// as that at the top of the stack
// then pop the top element
if (!st.isEmpty() &&
st.peek() == s.charAt(i))
st.pop();
// Else push the current character
// into the stack
else
st.add(s.charAt(i));
}
// If the stack is empty, then even
// palindromic subStrings are possible
if (st.isEmpty())
{
return true;
}
// Else not-possible
else
{
return false;
}
}
// Driver Code
public static void main(String[] args)
{
// Given String
String str = "aanncddc";
int n = str.length();
// Function Call
if (check(str, n))
{
System.out.print("Yes" + "\n");
}
else
{
System.out.print("No" + "\n");
}
}
}
// This code is contributed by sapnasingh4991
Python3
# Python3 program for the above approach
# Function to check string str can be
# split a string into even length
# palindromic substrings
def check(s, n):
st = []
# Iterate the string
for i in range(n):
# If the i-th character is same
# as that at the top of the stack
# then pop the top element
if (len(st) != 0 and
st[len(st) - 1] == s[i]):
st.pop();
# Else push the current character
# into the stack
else:
st.append(s[i]);
# If the stack is empty, then even
# palindromic substrings are possible
if (len(st) == 0):
return True;
# Else not-possible
else:
return False;
# Driver Code
# Given string
str = "aanncddc";
n = len(str)
# Function Call
if (check(str, n)):
print("Yes")
else:
print("No")
# This code is contributed by grand_master
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to check String str can be
// split a String into even length
// palindromic subStrings
static bool check(String s, int n)
{
// Initialize a stack
Stack<int> st = new Stack<int>();
// Iterate the String
for(int i = 0; i < n; i++)
{
// If the i-th character is same
// as that at the top of the stack
// then pop the top element
if (st.Count != 0 &&
st.Peek() == s[i])
st.Pop();
// Else push the current character
// into the stack
else
st.Push(s[i]);
}
// If the stack is empty, then even
// palindromic subStrings are possible
if (st.Count == 0)
{
return true;
}
// Else not-possible
else
{
return false;
}
}
// Driver Code
public static void Main(String[] args)
{
// Given String
String str = "aanncddc";
int n = str.Length;
// Function call
if (check(str, n))
{
Console.Write("Yes" + "\n");
}
else
{
Console.Write("No" + "\n");
}
}
}
// This code is contributed by sapnasingh4991
JavaScript
<script>
// JavaScript program for the above approach
// Function to check string str can be
// split a string into even length
// palindromic substrings
function check(s, n)
{
// Initialize a stack
var st = [];
// Iterate the string
for (var i = 0; i < n; i++) {
// If the i-th character is same
// as that at the top of the stack
// then pop the top element
if (st.length!=0 && st[st.length-1] == s[i])
st.pop();
// Else push the current character
// into the stack
else
st.push(s[i]);
}
// If the stack is empty, then even
// palindromic substrings are possible
if (st.length==0) {
return true;
}
// Else not-possible
else {
return false;
}
}
// Driver Code
// Given string
var str = "aanncddc";
var n = str.length;
// Function Call
if (check(str, n)) {
document.write( "Yes" );
}
else {
document.write( "No" );
}
</script>
Time Complexity: O(N), as we are using a loop for traversing the expression.
Auxiliary Space: O(N), as we are using stack for extra space.
Similar Reads
Check if a string contains a palindromic sub-string of even length S is string containing only lowercase English alphabets. We need to find if there exists at least one palindromic sub-string whose length is even. Examples: Input : aassssOutput : YESInput : gfgOutput : NOApproach: Approach to solve this problem is to check all even-length substrings of the given st
8 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
Check if K palindromic strings can be formed from a given string Given a string S of size N and an integer K, the task is to find whether the characters of the string can be arranged to make K palindromic strings simultaneously. Examples: Input: S = "annabelle", K = 2 Output: Yes Explanation: All characters of string S can be distributed into "elble" and "anna" w
7 min read
Check if a given string is a rotation of a palindrome Given a string, check if it is a rotation of a palindrome. For example your function should return true for "aab" as it is a rotation of "aba". Examples: Input: str = "aaaad" Output: 1 // "aaaad" is a rotation of a palindrome "aadaa" Input: str = "abcd" Output: 0 // "abcd" is not a rotation of any p
15+ 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 palindromic string can be obtained by concatenating substrings split from same indices of two given strings Given two strings A and B of length N, the task is to check if any of the two strings formed by splitting both the strings at any index i (0 ⤠i ⤠N - 1) and concatenating A[0, i] and B[i, N - 1] or A[i, N - 1] and B[0, i] respectively, form a palindromic string or not. If found to be true, print "Y
15+ min read