Open In App

Check if the string has a reversible equal substring at the ends

Last Updated : 25 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given a string S consisting of N characters, the task is to check if this string has a reversible equal substring from the start and the end. If yes, print True and then the longest substring present following the given conditions, otherwise print False.

Example:

Input: S = “abca”
Output: 
True
a
Explanation:
The substring “a”  is only the longest substring that  satisfy the given criteria. Therefore, print a.

Input: S = “acdfbcdca”
Output: 
True
acd
Explanation:
The substring “acd”  is only the longest substring that  satisfy the given criteria. Therefore, print acd.

Input: S = “abcdcb”
Output: False

Approach: The given problem can be solved by using the Two Pointer Approach. Following the steps below to solve the given problem:

  • Initialize a string, say ans as “” that stores the resultant string satisfying the given criteria.
  • Initialize two variables, say i and j as 0 and (N – 1) respectively.
  • Iterate a loop until j is non-negative and f the characters S[i] and S[j] are the same, then just add the character S[i] in the variable ans and increment the value of i by 1 and decrement the value of j by 1. Otherwise, break the loop.
  • After completing the above steps, if the string ans is empty then print False. Otherwise, print True and then print the string ans as the result.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to print longest substring
// that appears at beginning of string
// and also at end in reverse order
void commonSubstring(string s)
{
    int n = s.size();
    int i = 0;
    int j = n - 1;
 
    // Stores the resultant string
    string ans = "";
    while (j >= 0) {
 
        // If the characters are same
        if (s[i] == s[j]) {
            ans += s[i];
            i++;
            j--;
        }
 
        // Otherwise, break
        else {
            break;
        }
    }
 
    // If the string can't be formed
    if (ans.size() == 0)
        cout << "False";
 
    // Otherwise print resultant string
    else {
        cout << "True \n"
             << ans;
    }
}
 
// Driver Code
int main()
{
    string S = "abca";
    commonSubstring(S);
 
    return 0;
}


Java




// Java program for the above approach
 
public class GFG
{
   
    // Function to print longest substring
    // that appears at beginning of string
    // and also at end in reverse order
    static void commonSubstring(String s)
    {
        int n = s.length();
        int i = 0;
        int j = n - 1;
 
        // Stores the resultant string
        String ans = "";
        while (j >= 0) {
 
            // If the characters are same
            if (s.charAt(i) == s.charAt(j)) {
                ans += s.charAt(i);
                i++;
                j--;
            }
 
            // Otherwise, break
            else {
                break;
            }
        }
 
        // If the string can't be formed
        if (ans.length() == 0)
            System.out.println("False");
 
        // Otherwise print resultant string
        else {
            System.out.println("True ");
            System.out.println(ans);
        }
    }
 
    // Driver Code
    public static void main(String []args)
    {
        String S = "abca";
        commonSubstring(S);
    }
}
 
// This code is contributed by AnkThon


Python3




# python program for the above approach
 
# Function to print longest substring
# that appears at beginning of string
# and also at end in reverse order
def commonSubstring(s):
 
    n = len(s)
    i = 0
    j = n - 1
 
    # Stores the resultant string
    ans = ""
    while (j >= 0):
 
        # // If the characters are same
        if (s[i] == s[j]):
            ans += s[i]
            i = i + 1
            j = j - 1
 
        # Otherwise, break
        else:
            break
 
    # If the string can't be formed
    if (len(ans) == 0):
        print("False")
 
    # Otherwise print resultant string
    else:
        print("True")
        print(ans)
 
# Driver Code
if __name__ == "__main__":
 
    S = "abca"
    commonSubstring(S)
     
    # This code is contributed by rakeshsahni


C#




// C# program for the above approach
using System;
class GFG
{
   
    // Function to print longest substring
    // that appears at beginning of string
    // and also at end in reverse order
    static void commonSubstring(string s)
    {
        int n = s.Length;
        int i = 0;
        int j = n - 1;
 
        // Stores the resultant string
        string ans = "";
        while (j >= 0) {
 
            // If the characters are same
            if (s[i] == s[j]) {
                ans += s[i];
                i++;
                j--;
            }
 
            // Otherwise, break
            else {
                break;
            }
        }
 
        // If the string can't be formed
        if (ans.Length == 0)
            Console.WriteLine("False");
 
        // Otherwise print resultant string
        else {
            Console.WriteLine("True ");
            Console.WriteLine(ans);
        }
    }
 
    // Driver Code
    public static void Main()
    {
        string S = "abca";
        commonSubstring(S);
    }
}
 
// This code is contributed by ukasp.


Javascript




<script>
        // JavaScript Program to implement
        // the above approach
 
        // Function to print longest substring
        // that appears at beginning of string
        // and also at end in reverse order
        function commonSubstring(s) {
            let n = s.length;
            let i = 0;
            let j = n - 1;
 
            // Stores the resultant string
            let ans = "";
            while (j >= 0) {
 
                // If the characters are same
                if (s[i] == s[j]) {
                    ans += s[i];
                    i++;
                    j--;
                }
 
                // Otherwise, break
                else {
                    break;
                }
            }
 
            // If the string can't be formed
            if (ans.length == 0)
                document.write("False");
 
            // Otherwise print resultant string
            else {
                document.write("True" + "<br>"
                    + ans);
            }
        }
 
        // Driver Code
        let S = "abca";
        commonSubstring(S);
 
     // This code is contributed by Potta Lokesh
    </script>


Output

a

Time Complexity: O(N)
Auxiliary Space: O(N) because using extra space for string ans



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads