Check if the string has a reversible equal substring at the ends
Last Updated :
25 Sep, 2022
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++
#include <bits/stdc++.h>
using namespace std;
void commonSubstring(string s)
{
int n = s.size();
int i = 0;
int j = n - 1;
string ans = "" ;
while (j >= 0) {
if (s[i] == s[j]) {
ans += s[i];
i++;
j--;
}
else {
break ;
}
}
if (ans.size() == 0)
cout << "False" ;
else {
cout << "True \n"
<< ans;
}
}
int main()
{
string S = "abca" ;
commonSubstring(S);
return 0;
}
|
Java
public class GFG
{
static void commonSubstring(String s)
{
int n = s.length();
int i = 0 ;
int j = n - 1 ;
String ans = "" ;
while (j >= 0 ) {
if (s.charAt(i) == s.charAt(j)) {
ans += s.charAt(i);
i++;
j--;
}
else {
break ;
}
}
if (ans.length() == 0 )
System.out.println( "False" );
else {
System.out.println( "True " );
System.out.println(ans);
}
}
public static void main(String []args)
{
String S = "abca" ;
commonSubstring(S);
}
}
|
Python3
def commonSubstring(s):
n = len (s)
i = 0
j = n - 1
ans = ""
while (j > = 0 ):
if (s[i] = = s[j]):
ans + = s[i]
i = i + 1
j = j - 1
else :
break
if ( len (ans) = = 0 ):
print ( "False" )
else :
print ( "True" )
print (ans)
if __name__ = = "__main__" :
S = "abca"
commonSubstring(S)
|
C#
using System;
class GFG
{
static void commonSubstring( string s)
{
int n = s.Length;
int i = 0;
int j = n - 1;
string ans = "" ;
while (j >= 0) {
if (s[i] == s[j]) {
ans += s[i];
i++;
j--;
}
else {
break ;
}
}
if (ans.Length == 0)
Console.WriteLine( "False" );
else {
Console.WriteLine( "True " );
Console.WriteLine(ans);
}
}
public static void Main()
{
string S = "abca" ;
commonSubstring(S);
}
}
|
Javascript
<script>
function commonSubstring(s) {
let n = s.length;
let i = 0;
let j = n - 1;
let ans = "" ;
while (j >= 0) {
if (s[i] == s[j]) {
ans += s[i];
i++;
j--;
}
else {
break ;
}
}
if (ans.length == 0)
document.write( "False" );
else {
document.write( "True" + "<br>"
+ ans);
}
}
let S = "abca" ;
commonSubstring(S);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N) because using extra space for string ans
Share your thoughts in the comments
Please Login to comment...