📌  相关文章
📜  回文中最长的双字符串

📅  最后修改于: 2022-05-13 01:57:06.580000             🧑  作者: Mango

回文中最长的双字符串

给定一个回文字符串,任务是找到双字符串的最大长度以及可以从给定回文字符串中获得的长度。双字符串是一个子字符串一个接一个地有两个明确重复的字符串。
例子:

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

方法:双字符串可以考虑两种情况:

  • 情况1:如果字符串的长度是偶数,那么双字符串的长度将始终是字符串的长度。
  • 情况 2:如果字符串的长度是奇数,那么双字符串的长度将始终是字符串的长度 - 1。

下面是上述方法的实现

C++
#include 
using namespace std;
 
// Function to return the required position
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());
 
    // Print the double string
    cout << first_half << second_half << endl;
 
    // Print the length of the double string
    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
// Java implementation of the approach
class GFG
{
 
// Function to return the required position
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);
 
    // Print the double String
    System.out.println(first_half + second_half);
 
    // Print the length of the double String
    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--)
    {
        // Swap values of left and right
        char temp = temparray[left];
        temparray[left] = temparray[right];
        temparray[right] = temp;
    }
    return String.valueOf(temparray);
}
 
// Driver code
public static void main(String[] args)
{
    String n = "abba";
    lenDoubleString(n);
 
    n = "abcdedcba";
    lenDoubleString(n);
}
}
 
// This code contributed by Rajput-Ji


Python3
# Python3 implementation of the approach.
 
# Function to return the required position
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 the double string
    print(first_half + second_half)
 
    # Print the length of the double string
    if l % 2 == 0:
        print(l)
    else:
        print(l - 1)
 
# Driver Code
if __name__ == "__main__":
 
    n = "abba"
    lenDoubleString(n)
 
    n = "abcdedcba"
    lenDoubleString(n)
     
# This code is contributed by Rituraj Jain


C#
// C# implementation of the approach
using System;
 
class GFG
{
  
// Function to return the required position
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);
  
    // Print the double String
    Console.WriteLine(first_half + second_half);
  
    // Print the length of the double String
    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--)
    {
        // Swap values of left and right
        char temp = temparray[left];
        temparray[left] = temparray[right];
        temparray[right] = temp;
    }
    return String.Join("",temparray);
}
  
// Driver code
public static void Main(String[] args)
{
    String n = "abba";
    lenDoubleString(n);
  
    n = "abcdedcba";
    lenDoubleString(n);
}
}
 
// This code has been contributed by 29AjayKumar


PHP


Javascript


输出:
abab
4
abcdabcd
8

时间复杂度: O(1)。