Related Articles

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

• Last Updated : 17 Sep, 2021

Given a string S consisting of N characters, the task is to check if this string has an 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:

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

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 ``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

 ``
Output
`a`

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

My Personal Notes arrow_drop_up