Check if a string can be split into even length palindromic substrings
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++
#include <bits/stdc++.h>
using namespace std;
bool check(string s, int n)
{
stack< char > st;
for ( int i = 0; i < n; i++) {
if (!st.empty() && st.top() == s[i])
st.pop();
else
st.push(s[i]);
}
if (st.empty()) {
return true ;
}
else {
return false ;
}
}
int main()
{
string str = "aanncddc" ;
int n = str.length();
if (check(str, n)) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
return 0;
}
|
Java
import java.util.*;
class GFG{
static boolean check(String s, int n)
{
Stack<Character> st = new Stack<Character>();
for ( int i = 0 ; i < n; i++)
{
if (!st.isEmpty() &&
st.peek() == s.charAt(i))
st.pop();
else
st.add(s.charAt(i));
}
if (st.isEmpty())
{
return true ;
}
else
{
return false ;
}
}
public static void main(String[] args)
{
String str = "aanncddc" ;
int n = str.length();
if (check(str, n))
{
System.out.print( "Yes" + "\n" );
}
else
{
System.out.print( "No" + "\n" );
}
}
}
|
Python3
def check(s, n):
st = []
for i in range (n):
if ( len (st) ! = 0 and
st[ len (st) - 1 ] = = s[i]):
st.pop();
else :
st.append(s[i]);
if ( len (st) = = 0 ):
return True ;
else :
return False ;
str = "aanncddc" ;
n = len ( str )
if (check( str , n)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
using System.Collections.Generic;
class GFG{
static bool check(String s, int n)
{
Stack< int > st = new Stack< int >();
for ( int i = 0; i < n; i++)
{
if (st.Count != 0 &&
st.Peek() == s[i])
st.Pop();
else
st.Push(s[i]);
}
if (st.Count == 0)
{
return true ;
}
else
{
return false ;
}
}
public static void Main(String[] args)
{
String str = "aanncddc" ;
int n = str.Length;
if (check(str, n))
{
Console.Write( "Yes" + "\n" );
}
else
{
Console.Write( "No" + "\n" );
}
}
}
|
Javascript
<script>
function check(s, n)
{
var st = [];
for ( var i = 0; i < n; i++) {
if (st.length!=0 && st[st.length-1] == s[i])
st.pop();
else
st.push(s[i]);
}
if (st.length==0) {
return true ;
}
else {
return false ;
}
}
var str = "aanncddc" ;
var n = str.length;
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.
Last Updated :
24 May, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...