Longest double string from a Palindrome
Last Updated :
23 May, 2022
Given a palindrome String, the task is to find the maximum length of the double string and its length that can be obtained from the given palindromic string. A double string is a string that has two clear repetition of a substring one after the other.
Examples:
Input:
abba
Output:
abab
4
Explanation:
abab is double string
which can be obtained
by changing the order of letters
Input:
abcba
Output:
abab 4
Explanation:
abab is double string
which can be obtained
by changing the order of letters
and deleting letter c
Approach: The double string can be considered in two cases:
- Case 1: If the length of the string is even then the length of double string will always be the length of string.
- Case 2: If the length of the string is odd then the length of double string will always be the length of string – 1.
Below is the implementation of the above approach
C++
#include <bits/stdc++.h>
using namespace std;
int lenDoubleString(string s)
{
int l = s.length();
string first_half = s.substr(0, l / 2);
string second_half = "" ;
if (l % 2 == 0)
second_half = s.substr(l / 2);
else
second_half = s.substr(l / 2 + 1);
reverse(second_half.begin(), second_half.end());
cout << first_half << second_half << endl;
if (l % 2 == 0)
cout << l << endl;
else
cout << l - 1 << endl;
}
int main()
{
string n = "abba" ;
lenDoubleString(n);
n = "abcdedcba" ;
lenDoubleString(n);
return 0;
}
|
Java
class GFG
{
static int lenDoubleString(String s)
{
int l = s.length();
String first_half = s.substring( 0 , l / 2 );
String second_half = "" ;
if (l % 2 == 0 )
second_half = s.substring(l / 2 );
else
second_half = s.substring(l / 2 + 1 );
second_half = reverse(second_half);
System.out.println(first_half + second_half);
if (l % 2 == 0 )
System.out.println(l);
else
System.out.println(l - 1 );
return Integer.MIN_VALUE;
}
static String reverse(String input)
{
char [] temparray = input.toCharArray();
int left, right = 0 ;
right = temparray.length - 1 ;
for (left = 0 ; left < right; left++, right--)
{
char temp = temparray[left];
temparray[left] = temparray[right];
temparray[right] = temp;
}
return String.valueOf(temparray);
}
public static void main(String[] args)
{
String n = "abba" ;
lenDoubleString(n);
n = "abcdedcba" ;
lenDoubleString(n);
}
}
|
Python3
def lenDoubleString(s):
l = len (s)
first_half = s[ 0 : l / / 2 ]
second_half = ""
if l % 2 = = 0 :
second_half = s[l / / 2 : ]
else :
second_half = s[l / / 2 + 1 : ]
second_half = second_half[:: - 1 ]
print (first_half + second_half)
if l % 2 = = 0 :
print (l)
else :
print (l - 1 )
if __name__ = = "__main__" :
n = "abba"
lenDoubleString(n)
n = "abcdedcba"
lenDoubleString(n)
|
C#
using System;
class GFG
{
static int lenDoubleString(String s)
{
int l = s.Length;
String first_half = s.Substring(0, l / 2);
String second_half = "" ;
if (l % 2 == 0)
second_half = s.Substring(l / 2);
else
second_half = s.Substring(l / 2 + 1);
second_half = reverse(second_half);
Console.WriteLine(first_half + second_half);
if (l % 2 == 0)
Console.WriteLine(l);
else
Console.WriteLine(l - 1);
return int .MinValue;
}
static String reverse(String input)
{
char [] temparray = input.ToCharArray();
int left, right = 0;
right = temparray.Length - 1;
for (left = 0; left < right; left++, right--)
{
char temp = temparray[left];
temparray[left] = temparray[right];
temparray[right] = temp;
}
return String.Join( "" ,temparray);
}
public static void Main(String[] args)
{
String n = "abba" ;
lenDoubleString(n);
n = "abcdedcba" ;
lenDoubleString(n);
}
}
|
PHP
<?php
function lenDoubleString( $s )
{
$l = strlen ( $s );
$first_half = substr ( $s , 0, (int)( $l / 2));
$second_half = "" ;
if ( $l % 2 == 0)
$second_half = substr ( $s , (int)( $l / 2));
else
$second_half = substr ( $s , (int)( $l / 2 + 1));
$second_half = strrev ( $second_half );
echo $first_half . "" .
$second_half . "\n" ;
if ( $l % 2 == 0)
echo $l . "\n" ;
else
echo ( $l - 1) . "\n" ;
}
$n = "abba" ;
lenDoubleString( $n );
$n = "abcdedcba" ;
lenDoubleString( $n );
?>
|
Javascript
<script>
function lenDoubleString(s)
{
var l = s.length;
var first_half = s.substr(0, l / 2);
var second_half = "" ;
if (l % 2 == 0)
second_half = s.substr(l / 2);
else
second_half = s.substr(l / 2 + 1);
second_half = second_half.split( "" ).reverse().join( "" );
document.write(first_half);
document.write(second_half+ "<br>" );
if (l % 2 == 0)
document.write(l+ "<br>" );
else
document.write(l - 1+ "<br>" );
}
var n = "abba" ;
lenDoubleString(n);
n = "abcdedcba" ;
lenDoubleString(n);
</script>
|
Output:
abab
4
abcdabcd
8
Time Complexity: O(N), as we are using built-in functions reverse which will cost O(N) time.
Auxiliary Space: O(N), as we are using extra space.
Share your thoughts in the comments
Please Login to comment...