Open In App

Split string into three palindromic substrings with earliest possible cuts

Improve
Improve
Like Article
Like
Save
Share
Report

Given string str, the task is to check if it is possible to split the given string S into three palindromic substrings or not. If multiple answers are possible, then print the one where the cuts are made the least indices. If no such possible partition exists, then print “-1”.

Examples:

Input: str = “aabbcdc”
Output: aa bb cdc
Explanation:
Only one possible partition exists {“aa”, “bb”, “cdc”}.

Input: str = “ababbcb”
Output: a bab bcb 
Explanation: Possible splits are {“aba”, “b”, “bcb”} and {“a”, “bab”, “bcb”}. 
Since, {“a”, “bab”, “bcb”} has splits at earlier indices, it is the required answer.

Brute Force Approach:

  1. Use two nested loops, first loop for finding the first palindrome and second loop for finding the middle and last palindrome.
  2. Run the first loop from i =  1 to str.length-2 and check if substring str1[0 to i-1] is a palindrome or not.
  3. If the substring str1[0 to i-1] is palindrome then run another loop j = i+1 to str.length-1 and check if substring2 str2[i to j-1] and substring3 str3[j to str.length-1] is palindrome or not.
  4. If substring2 str2[i to j-1] and substring3 str3[j to str.length-1] is palindrome then print str1, str2 and str3 else if there is no such split print -1 at last.

C++




#include <iostream>
 
using namespace std;
 
bool isPalindrome(const string& s) {
    return s == string(s.rbegin(), s.rend());
}
 
void solve(const string& s) {
    int n = s.length();
 
    // Loop to check if the substring s1[0 to i-1] is palindrome or not
    for (int i = 1; i < n - 1; ++i) {
        string s1 = s.substr(0, i);
        // if substring s1[0 to i-1] is palindrome then check for s2 and s3
        if (isPalindrome(s1)) {
            // Loop to check substring2 (s2[i to j-1]) and substring3 (s3[j to n-1])
          // is palindrome or not
            for (int j = i + 1; j < n; ++j) {
                string s2 = s.substr(i, j - i);
                string s3 = s.substr(j);
                // if s2[i to j-1] and s3[j to n-1] is palindrome then
                // string s can be split into three substrings hence print them and return
                if (isPalindrome(s2) && isPalindrome(s3)) {
                    cout << s1 << " " << s2 << " " << s3 << endl;
                    return;
                }
            }
        }
    }
    // Given string s cannot be split into three palindromic substrings hence print -1 and return
    cout << -1 << endl;
}
 
int main() {
    string s = "ababbcb";
    solve(s);
    return 0;
}


Java




import java.util.*;
 
public class GFG {
    // Function to check if a string is a palindrome
    public static boolean isPalindrome(String s) {
        return s.equals(new StringBuilder(s).reverse().toString());
    }
 
    // Function to find and print three palindromic substrings from the given string
    public static void solve(String s) {
        int n = s.length();
 
        // Loop to check if the substring s1[0 to i-1] is palindrome or not
        for (int i = 1; i < n - 1; ++i) {
            String s1 = s.substring(0, i);
            // If substring s1[0 to i-1] is a palindrome, then check for s2 and s3
            if (isPalindrome(s1)) {
                // Loop to check substring2 (s2[i to j-1]) and substring3 (s3[j to n-1]) is palindrome or not
                for (int j = i + 1; j < n; ++j) {
                    String s2 = s.substring(i, j);
                    String s3 = s.substring(j);
                    // If s2[i to j-1] and s3[j to n-1] are palindromes, then the string s can be split into three substrings
                    // hence print them and return
                    if (isPalindrome(s2) && isPalindrome(s3)) {
                        System.out.println(s1 + " " + s2 + " " + s3);
                        return;
                    }
                }
            }
        }
        // Given string s cannot be split into three palindromic substrings hence print -1 and return
        System.out.println(-1);
    }
 
    // Driver Code
    public static void main(String[] args) {
        String s = "ababbcb";
        solve(s);
    }
}


Python3




# Function to check if string s is palindrome or not
def isPalindrome(s):
    return s == s[::-1]
 
# Function to solve the problem
def solve(s):
    = len(s)         # storing the length of string
    # Loop to check if the substring s1[0 to i-1] is palindrome or not
    for i in range(1,n-2):
        s1 = s[:i]
        # if substring s1[0 to i-1] is palindrome then check for s2 and s3
        if isPalindrome(s1):
            # Loop to check substring2 (s2[i to j-1]) and substring3 (s3[j to n-1]) is palindrome or not
            for j in range(i+1,n-1):
                s2 = s[i:j]
                s3 = s[j:]
                # if s2[i to j-1] and s3[j to n-1] is palindrome then
                # string s can be split into three substrings hence print them and return
                if isPalindrome(s2) and isPalindrome(s3):
                    print(s1, s2, s3)
                    return
    # Given string s cannot be split into three palindromic substrings hence print -1 and return
    print(-1)
     
s =  "ababbcb"
solve(s)


C#




using System;
 
class Program
{
    // Function to check if string s is palindrome or not
    static bool IsPalindrome(string s)
    {
        for (int i = 0; i < s.Length / 2; i++)
        {
            if (s[i] != s[s.Length - i - 1])
            {
                return false;
            }
        }
        return true;
    }
 
    // Function to solve the problem
    static void Solve(string s)
    {
        int n = s.Length; // storing the length of string
        // Loop to check if the substring s1[0 to i-1] is palindrome or not
        for (int i = 1; i < n - 1; i++)
        {
            string s1 = s.Substring(0, i);
            // if substring s1[0 to i-1] is palindrome then check for s2 and s3
            if (IsPalindrome(s1))
            {
                // Loop to check substring2 (s2[i to j-1]) and substring3 (s3[j to n-1]) is palindrome or not
                for (int j = i + 1; j < n; j++)
                {
                    string s2 = s.Substring(i, j - i);
                    string s3 = s.Substring(j);
                    // if s2[i to j-1] and s3[j to n-1] is palindrome then
                    // string s can be split into three substrings hence print them and return
                    if (IsPalindrome(s2) && IsPalindrome(s3))
                    {
                        Console.WriteLine(s1 + " " + s2 + " " + s3);
                        return;
                    }
                }
            }
        }
         
        Console.WriteLine(-1);
    }
// driver code
    static void Main(string[] args)
    {
        string s = "ababbcb";
        Solve(s);
    }
}


Javascript




function isPalindrome(s) {
    return s === s.split('').reverse().join('');
}
 
function solve(s) {
    const n = s.length;
 
    // Loop to check if the substring s1[0 to i-1] is palindrome or not
    for (let i = 1; i < n - 1; ++i) {
        const s1 = s.substring(0, i);
 
        // if substring s1[0 to i-1] is palindrome then check for s2 and s3
        if (isPalindrome(s1)) {
            // Loop to check substring2 (s2[i to j-1]) and substring3 (s3[j to n-1])
            // is palindrome or not
            for (let j = i + 1; j < n; ++j) {
                const s2 = s.substring(i, j);
                const s3 = s.substring(j);
 
                // if s2[i to j-1] and s3[j to n-1] is palindrome then
                // string s can be split into three substrings hence print them and return
                if (isPalindrome(s2) && isPalindrome(s3)) {
                    console.log(s1, s2, s3);
                    return;
                }
            }
        }
    }
 
    // Given string s cannot be split into three palindromic substrings hence print -1 and return
    console.log(-1);
}
 
const s = "ababbcb";
solve(s);


Output

a bab bcb








Time Complexity: O(N3)
Auxiliary Space: O(1)

Optimized Approach: Follow the steps below to solve the problem:

  1. Generate all possible substrings of the string and check whether the given substring is palindromic or not.
  2. If any substring is present then store the last index of a substring in a vector startPal, where startPal will store the first palindrome starting from 0 indexes and ending at stored value.
  3. Similar to the first step, generate all the substring from the last index of the given string str and check whether the given substring is palindrome or not. If any substring is present as a substring then store the last index of a substring in the vector lastPal, where lastPal will store the third palindrome starting from stored value and ending at (N – 1)th index of the given string str.
  4. Reverse the vector lastPal to get the earliest cut.
  5. Now, iterate two nested loop, the outer loop picks the first palindrome ending index from the startPal and the inner loop picks the third palindrome starting index from the lastPal. If the value of startPal is lesser than the lastPal value then store it in the middlePal vector in form of pairs.
  6. Now traverse over the vector middlePal and check if the substring between the ending index of the first palindrome and starting index of the third palindrome is palindrome or not. If found to be true then the first palindrome = s.substr(0, middlePal[i].first), third palindrome = s.substr(middlePal[i].second, N – middlePal[i].second) and rest string will be third string.
  7. If all the three palindromic strings are present then print that string Otherwise print “-1”.

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 if a string
// is palindrome or not
bool isPalindrome(string x)
{
    // Copy the string x to y
    string y = x;
 
    // Reverse the string y
    reverse(y.begin(), y.end());
 
    if (x == y) {
 
        // If string x equals y
        return true;
    }
 
    return false;
}
 
// Function to find three palindromes
// from the given string with earliest
// possible cuts
void Palindromes(string S, int N)
{
    // Stores index of palindrome
    // starting from left & right
    vector<int> startPal, lastPal;
 
    string start;
 
    // Store the indices for possible
    // palindromes from left
    for (int i = 0;
         i < S.length() - 2; i++) {
 
        // Push the current character
        start.push_back(S[i]);
 
        // Check for palindrome
        if (isPalindrome(start)) {
 
            // Insert the current index
            startPal.push_back(i);
        }
    }
 
    string last;
 
    // Stores the indexes for possible
    // palindromes from right
    for (int j = S.length() - 1;
         j >= 2; j--) {
 
        // Push the current character
        last.push_back(S[j]);
 
        // Check palindromic
        if (isPalindrome(last)) {
 
            // Insert the current index
            lastPal.push_back(j);
        }
    }
 
    // Sort the indexes for palindromes
    // from right in ascending order
    reverse(lastPal.begin(),
            lastPal.end());
 
    vector<pair<int, int> > middlePal;
 
    for (int i = 0;
         i < startPal.size(); i++) {
 
        for (int j = 0;
             j < lastPal.size(); j++) {
 
            // If the value of startPal
            // < lastPal value then
            // store it in middlePal
            if (startPal[i] < lastPal[j]) {
 
                // Insert the current pair
                middlePal.push_back(
                    { startPal[i],
                      lastPal[j] });
            }
        }
    }
 
    string res1, res2, res3;
    int flag = 0;
 
    // Traverse over the middlePal
    for (int i = 0;
         i < middlePal.size(); i++) {
 
        int x = middlePal[i].first;
        int y = middlePal[i].second;
 
        string middle;
 
        for (int k = x + 1; k < y; k++) {
            middle.push_back(S[k]);
        }
 
        // Check if the middle part
        // is palindrome
        if (isPalindrome(middle)) {
            flag = 1;
            res1 = S.substr(0, x + 1);
            res2 = middle;
            res3 = S.substr(y, N - y);
            break;
        }
    }
 
    // Print the three palindromic
    if (flag == 1) {
        cout << res1 << " "
             << res2 << " "
             << res3;
    }
 
    // Otherwise
    else
        cout << "-1";
}
 
// Driver Code
int main()
{
    // Given string str
    string str = "ababbcb";
 
    int N = str.length();
 
    // Function Call
    Palindromes(str, N);
 
    return 0;
}


Java




// Java program for the
// above approach
import java.util.*;
class GFG{
 
static class pair
{
  int first, second;
  public pair(int first,
              int second)
  {
    this.first = first;
    this.second = second;
  }
}
 
static String reverse(String input)
{
  char[] a = input.toCharArray();
  int l, r = a.length - 1;
   
  for (l = 0; l < r; l++, r--)
  {
    char temp = a[l];
    a[l] = a[r];
    a[r] = temp;
  }
  return String.valueOf(a);
}
 
// Function to check if a String
// is palindrome or not
static boolean isPalindrome(String x)
{
  // Copy the String x to y
  String y = x;
 
  // Reverse the String y
  y = reverse(y);
 
  if (x.equals(y))
  {
    // If String x equals y
    return true;
  }
 
  return false;
}
 
// Function to find three palindromes
// from the given String with earliest
// possible cuts
static void Palindromes(String S,
                        int N)
{
  // Stores index of palindrome
  // starting from left & right
  Vector<Integer> startPal =
         new Vector<>();
  Vector<Integer> lastPal =
         new Vector<>();
 
  String start = "";
 
  // Store the indices for possible
  // palindromes from left
  for (int i = 0;
           i < S.length() - 2; i++)
  {
    // Push the current character
    start += S.charAt(i);
 
    // Check for palindrome
    if (isPalindrome(start))
    {
      // Insert the current index
      startPal.add(i);
    }
  }
 
  String last = "";
 
  // Stores the indexes for possible
  // palindromes from right
  for (int j = S.length() - 1;
           j >= 2; j--)
  {
    // Push the current character
    last += S.charAt(j);
 
    // Check palindromic
    if (isPalindrome(last))
    {
      // Insert the current index
      lastPal.add(j);
    }
  }
 
  // Sort the indexes for palindromes
  // from right in ascending order
  Collections.reverse(lastPal);
 
  Vector<pair> middlePal =
               new Vector<>();
   
  for (int i = 0;
           i < startPal.size(); i++)
  {
    for (int j = 0;
             j < lastPal.size(); j++)
    {
      // If the value of startPal
      // < lastPal value then
      // store it in middlePal
      if (startPal.get(i) <
          lastPal.get(j))
      {
        // Insert the current pair
        middlePal.add(new pair(startPal.get(i),
                               lastPal.get(j)));
      }
    }
  }
 
  String res1 = "",
         res2 = "", res3 = "";
  int flag = 0;
 
  // Traverse over the middlePal
  for (int i = 0;
           i < middlePal.size(); i++)
  {
    int x = middlePal.get(i).first;
    int y = middlePal.get(i).second;
    String middle = "";
 
    for (int k = x + 1; k < y; k++)
    {
      middle += S.charAt(k);
    }
 
    // Check if the middle part
    // is palindrome
    if (isPalindrome(middle))
    {
      flag = 1;
      res1 = S.substring(0, x + 1);
      res2 = middle;
      res3 = S.substring(y, N);
      break;
    }
  }
 
  // Print the three palindromic
  if (flag == 1)
  {
    System.out.print(res1 + " " +
                     res2 + " " + res3);
  }
 
  // Otherwise
  else
    System.out.print("-1");
}
 
// Driver Code
public static void main(String[] args)
{
  // Given String str
  String str = "ababbcb";
 
  int N = str.length();
 
  // Function Call
  Palindromes(str, N);
}
}
 
// This code is contributed by shikhasingrajput


Python3




# Python3 program for the above approach
 
# Function to check if a string
# is palindrome or not
def isPalindrome(x):
     
    # Copy the x to y
    y = x
 
    # Reverse the y
    y = y[::-1]
 
    if (x == y):
 
        # If x equals y
        return True
         
    return False
 
# Function to find three palindromes
# from the given with earliest
# possible cuts
def Palindromes(S, N):
     
    # Stores index of palindrome
    # starting from left & right
    startPal, lastPal = [], []
 
    start = []
 
    # Store the indices for possible
    # palindromes from left
    for i in range(len(S) - 2):
 
        # Push the current character
        start.append(S[i])
 
        # Check for palindrome
        if (isPalindrome(start)):
 
            # Insert the current index
            startPal.append(i)
 
    last = []
 
    # Stores the indexes for possible
    # palindromes from right
    for j in range(len(S) - 1, 1, -1):
 
        # Push the current character
        last.append(S[j])
 
        # Check palindromic
        if (isPalindrome(last)):
 
            # Insert the current index
            lastPal.append(j)
 
    # Sort the indexes for palindromes
    # from right in ascending order
    lastPal = lastPal[::-1]
 
    middlePal = []
 
    for i in range(len(startPal)):
        for j in range(len(lastPal)):
 
            # If the value of startPal
            # < lastPal value then
            # store it in middlePal
            if (startPal[i] < lastPal[j]):
 
                # Insert the current pair
                middlePal.append([startPal[i],
                                   lastPal[j]])
 
    res1, res2, res3 = "", "", ""
    flag = 0
 
    # Traverse over the middlePal
    for i in range(len(middlePal)):
        x = middlePal[i][0]
        y = middlePal[i][1]
        #print(x,y)
 
        middle = ""
 
        for k in range(x + 1, y):
            middle += (S[k])
 
        # Check if the middle part
        # is palindrome
        if (isPalindrome(middle)):
            flag = 1
            res1 = S[0 : x + 1]
            res2 = middle
            res3 = S[y : N]
            #print(S,x,y,N)
            break
 
    # Print the three palindromic
    if (flag == 1):
        print(res1, res2, res3)
 
    # Otherwise
    else:
        print("-1")
 
# Driver Code
if __name__ == '__main__':
     
    # Given strr
    strr = "ababbcb"
 
    N = len(strr)
 
    # Function call
    Palindromes(strr, N)
 
# This code is contributed by mohit kumar 29


C#




// C# program for the
// above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
public class pair
{
  public int first, second;
  public pair(int first,
              int second)
  {
    this.first = first;
    this.second = second;
  }
}
 
static String reverse(String input)
{
  char[] a = input.ToCharArray();
  int l, r = a.Length - 1;
   
  for(l = 0; l < r; l++, r--)
  {
    char temp = a[l];
    a[l] = a[r];
    a[r] = temp;
  }
  return String.Join("",a);
}
 
// Function to check if a String
// is palindrome or not
static bool isPalindrome(String x)
{
   
  // Copy the String x to y
  String y = x;
 
  // Reverse the String y
  y = reverse(y);
 
  if (x.Equals(y))
  {
     
    // If String x equals y
    return true;
  }
  return false;
}
 
// Function to find three palindromes
// from the given String with earliest
// possible cuts
static void Palindromes(String S,
                        int N)
{
   
  // Stores index of palindrome
  // starting from left & right
  List<int> startPal = new List<int>();
  List<int> lastPal = new List<int>();
   
  String start = "";
 
  // Store the indices for possible
  // palindromes from left
  for(int i = 0;
          i < S.Length - 2; i++)
  {
     
    // Push the current character
    start += S[i];
 
    // Check for palindrome
    if (isPalindrome(start))
    {
       
      // Insert the current index
      startPal.Add(i);
    }
  }
 
  String last = "";
 
  // Stores the indexes for possible
  // palindromes from right
  for(int j = S.Length - 1;
          j >= 2; j--)
  {
     
    // Push the current character
    last += S[j];
 
    // Check palindromic
    if (isPalindrome(last))
    {
       
      // Insert the current index
      lastPal.Add(j);
    }
  }
   
  // Sort the indexes for palindromes
  // from right in ascending order
  lastPal.Reverse();
 
  List<pair> middlePal = new List<pair>();
   
  for(int i = 0;
          i < startPal.Count; i++)
  {
    for(int j = 0;
            j < lastPal.Count; j++)
    {
       
      // If the value of startPal
      // < lastPal value then
      // store it in middlePal
      if (startPal[i] < lastPal[j])
      {
         
        // Insert the current pair
        middlePal.Add(new pair(startPal[i],
                                lastPal[j]));
      }
    }
  }
 
  String res1 = "", res2 = "",
         res3 = "";
  int flag = 0;
 
  // Traverse over the middlePal
  for(int i = 0;
          i < middlePal.Count; i++)
  {
    int x = middlePal[i].first;
    int y = middlePal[i].second;
    String middle = "";
 
    for(int k = x + 1; k < y; k++)
    {
      middle += S[k];
    }
 
    // Check if the middle part
    // is palindrome
    if (isPalindrome(middle))
    {
      flag = 1;
      res1 = S.Substring(0, x + 1);
      res2 = middle;
      res3 = S.Substring(y);
      break;
    }
  }
 
  // Print the three palindromic
  if (flag == 1)
  {
    Console.Write(res1 + " " +
                  res2 + " " + res3);
  }
 
  // Otherwise
  else
    Console.Write("-1");
}
 
// Driver Code
public static void Main(String[] args)
{
   
  // Given String str
  String str = "ababbcb";
 
  int N = str.Length;
 
  // Function Call
  Palindromes(str, N);
}
}
 
// This code is contributed by Amit Katiyar


Javascript




<script>
      // Function to check if a String
      // is palindrome or not
      function isPalindrome(x) {
        // Copy the String x to y
        var y = x;
 
        // Reverse the String y
        y = y.split("").reverse().join("");
 
        if (x === y) {
          // If String x equals y
          return true;
        }
        return false;
      }
 
      // Function to find three palindromes
      // from the given String with earliest
      // possible cuts
      function Palindromes(S, N) {
        // Stores index of palindrome
        // starting from left & right
        var startPal = [];
        var lastPal = [];
 
        var start = "";
 
        // Store the indices for possible
        // palindromes from left
        for (var i = 0; i < S.length - 2; i++) {
          // Push the current character
          start += S[i];
 
          // Check for palindrome
          if (isPalindrome(start)) {
            // Insert the current index
            startPal.push(i);
          }
        }
 
        var last = "";
 
        // Stores the indexes for possible
        // palindromes from right
        for (var j = S.length - 1; j >= 2; j--) {
          // Push the current character
          last += S[j];
 
          // Check palindromic
          if (isPalindrome(last)) {
            // Insert the current index
            lastPal.push(j);
          }
        }
 
        // Sort the indexes for palindromes
        // from right in ascending order
        lastPal.sort();
 
        var middlePal = [];
 
        for (var i = 0; i < startPal.length; i++) {
          for (var j = 0; j < lastPal.length; j++) {
            // If the value of startPal
            // < lastPal value then
            // store it in middlePal
            if (startPal[i] < lastPal[j]) {
              // Insert the current pair
              middlePal.push([startPal[i], lastPal[j]]);
            }
          }
        }
 
        var res1 = "",
          res2 = "",
          res3 = "";
        var flag = 0;
 
        // Traverse over the middlePal
        for (var i = 0; i < middlePal.length; i++) {
          var x = middlePal[i][0];
          var y = middlePal[i][1];
          var middle = "";
 
          for (var k = x + 1; k < y; k++) {
            middle += S[k];
          }
 
          // Check if the middle part
          // is palindrome
          if (isPalindrome(middle)) {
            flag = 1;
            res1 = S.substring(0, x + 1);
            res2 = middle;
            res3 = S.substring(y, N);
            break;
          }
        }
 
        // Print the three palindromic
        if (flag === 1) {
          document.write(res1 + " " + res2 + " " + res3);
        }
 
        // Otherwise
        else document.write("-1");
      }
 
      // Driver Code
      // Given String str
      var str = "ababbcb";
 
      var N = str.length;
 
      // Function Call
      Palindromes(str, N);
</script>


Output

a bab bcb







Time Complexity: O(N2)
Auxiliary Space: O(N)



Last Updated : 09 Oct, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads