📌  相关文章
📜  要在前面添加最少字符以生成字符串回文

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

要在前面添加最少字符以生成字符串回文

给定一个字符串str,我们需要告诉在字符串前面添加的最少字符以使字符串回文。
例子:

Input  : str = "ABC"
Output : 2
We can make above string palindrome as "CBABC"
by adding 'B' and 'C' at front.

Input  : str = "AACECAAAA";
Output : 2
We can make above string palindrome as AAAACECAAAA
by adding two A's at front of string.

幼稚的方法:每次都开始检查字符串是否是回文,如果不是,则删除最后一个字符并再次检查。当字符串减少到回文或空字符串时,从结尾到现在删除的字符数将是答案,因为这些字符可能已按顺序插入到原始字符串的开头,这将使字符串成为回文。
下面是上述方法的实现:

C++
// C++ program for getting minimum character to be
// added at front to make string palindrome
#include
using namespace std;
 
// function for checking string is palindrome or not
bool ispalindrome(string s)
{
    int l = s.length();
    int j;
     
    for(int i = 0, j = l - 1; i <= j; i++, j--)
    {
        if(s[i] != s[j])
            return false;
    }
    return true;
}
 
// Driver code
int main()
{
    string s = "BABABAA";
    int cnt = 0;
    int flag = 0;
     
    while(s.length()>0)
    {
        // if string becomes palindrome then break
        if(ispalindrome(s))
        {
            flag = 1;
             break;
        }
        else
        {
        cnt++;
         
        // erase the last element of the string
        s.erase(s.begin() + s.length() - 1);
        }
    }
     
    // print the number of insertion at front
    if(flag)
        cout << cnt;
}


Java
// Java program for getting minimum character to be
// added at front to make string palindrome
 
class GFG {
 
// function for checking string is palindrome or not
    static boolean ispalindrome(String s) {
        int l = s.length();
 
        for (int i = 0, j = l - 1; i <= j; i++, j--) {
            if (s.charAt(i) != s.charAt(j)) {
                return false;
            }
        }
        return true;
    }
 
// Driver code
    public static void main(String[] args) {
        String s = "BABABAA";
        int cnt = 0;
        int flag = 0;
 
        while (s.length() > 0) {
            // if string becomes palindrome then break
            if (ispalindrome(s)) {
                flag = 1;
                break;
            } else {
                cnt++;
 
                // erase the last element of the string
                s = s.substring(0, s.length() - 1);
                //s.erase(s.begin() + s.length() - 1);
            }
        }
 
        // print the number of insertion at front
        if (flag == 1) {
            System.out.println(cnt);
        }
    }
}
 
// This code is contributed by 29AjayKumar


Python 3
# Python 3 program for getting minimum character
# to be added at front to make string palindrome
 
# function for checking string is
# palindrome or not
def ispalindrome(s):
 
    l = len(s)
     
    i = 0
    j = l - 1
    while i <= j:
     
        if(s[i] != s[j]):
            return False
        i += 1
        j -= 1
     
    return True
 
# Driver code
if __name__ == "__main__":
     
    s = "BABABAA"
    cnt = 0
    flag = 0
     
    while(len(s) > 0):
     
        # if string becomes palindrome then break
        if(ispalindrome(s)):
            flag = 1
            break
         
        else:
            cnt += 1
         
            # erase the last element of the string
            s = s[:-1]
     
    # print the number of insertion at front
    if(flag):
        print(cnt)
 
# This code is contributed by ita_c


C#
// C# program for getting minimum character to be
// added at front to make string palindrome
 
using System;
public class GFG {
 
// function for checking string is palindrome or not
    static bool ispalindrome(String s) {
        int l = s.Length;
 
        for (int i = 0, j = l - 1; i <= j; i++, j--) {
            if (s[i] != s[j]) {
                return false;
            }
        }
        return true;
    }
 
// Driver code
    public static void Main() {
        String s = "BABABAA";
        int cnt = 0;
        int flag = 0;
 
        while (s.Length > 0) {
            // if string becomes palindrome then break
            if (ispalindrome(s)) {
                flag = 1;
                break;
            } else {
                cnt++;
 
                // erase the last element of the string
                s = s.Substring(0, s.Length - 1);
                //s.erase(s.begin() + s.length() - 1);
            }
        }
 
        // print the number of insertion at front
        if (flag == 1) {
            Console.WriteLine(cnt);
        }
    }
}
 
// This code is contributed by PrinciRaj1992


Javascript


C++
// C++ program for getting minimum character to be
// added at front to make string palindrome
#include 
using namespace std;
 
// returns vector lps for given string str
vector computeLPSArray(string str)
{
    int M = str.length();
    vector lps(M);
 
    int len = 0;
    lps[0] = 0; // lps[0] is always 0
 
    // the loop calculates lps[i] for i = 1 to M-1
    int i = 1;
    while (i < M)
    {
        if (str[i] == str[len])
        {
            len++;
            lps[i] = len;
            i++;
        }
        else // (str[i] != str[len])
        {
            // This is tricky. Consider the example.
            // AAACAAAA and i = 7. The idea is similar
            // to search step.
            if (len != 0)
            {
                len = lps[len-1];
 
                // Also, note that we do not increment
                // i here
            }
            else // if (len == 0)
            {
                lps[i] = 0;
                i++;
            }
        }
    }
    return lps;
}
 
// Method returns minimum character to be added at
// front to make string palindrome
int getMinCharToAddedToMakeStringPalin(string str)
{
    string revStr = str;
    reverse(revStr.begin(), revStr.end());
 
    // Get concatenation of string, special character
    // and reverse string
    string concat = str + "$" + revStr;
 
    //  Get LPS array of this concatenated string
    vector lps = computeLPSArray(concat);
 
    // By subtracting last entry of lps vector from
    // string length, we will get our result
    return (str.length() - lps.back());
}
 
// Driver program to test above functions
int main()
{
    string str = "AACECAAAA";
    cout << getMinCharToAddedToMakeStringPalin(str);
    return 0;
}


Java
// Java program for getting minimum character to be
// added at front to make string palindrome
import java.util.*;
class GFG
{
 
// returns vector lps for given string str
public static int[] computeLPSArray(String str)
{
    int n = str.length();
    int lps[] = new int[n];
    int i = 1, len = 0;
    lps[0] = 0; // lps[0] is always 0
     
    while (i < n)
    {
        if (str.charAt(i) == str.charAt(len))
        {
            len++;
            lps[i] = len;
            i++;
        }
        else
        {
            // This is tricky. Consider the example.
            // AAACAAAA and i = 7. The idea is similar
            // to search step.
            if (len != 0)
            {
                len = lps[len - 1];
                 
                // Also, note that we do not increment
                // i here
            }
            else
            {
                lps[i] = 0;
                i++;
            }
        }
    }
    return lps;
}
 
// Method returns minimum character to be added at
// front to make string palindrome
static int getMinCharToAddedToMakeStringPalin(String str)
{
    StringBuilder s = new StringBuilder();
    s.append(str);
     
    // Get concatenation of string, special character
    // and reverse string
    String rev = s.reverse().toString();
    s.reverse().append("$").append(rev);
     
    // Get LPS array of this concatenated string
    int lps[] = computeLPSArray(s.toString());
    return str.length() - lps[s.length() - 1];
}
 
// Driver Code
public static void main(String args[])
{
    String str = "AACECAAAA";
    System.out.println(getMinCharToAddedToMakeStringPalin(str));
}
}
 
// This code is contributed by Sparsh Singhal


Python3
# Python3 program for getting minimum
# character to be added at the front
# to make string palindrome
 
# Returns vector lps for given string str
def computeLPSArray(string):
 
    M = len(string)
    lps = [None] * M
 
    length = 0
    lps[0] = 0 # lps[0] is always 0
 
    # the loop calculates lps[i]
    # for i = 1 to M-1
    i = 1
    while i < M:
     
        if string[i] == string[length]:
         
            length += 1
            lps[i] = length
            i += 1
         
        else: # (str[i] != str[len])
         
            # This is tricky. Consider the example.
            # AAACAAAA and i = 7. The idea is
            # similar to search step.
            if length != 0:
             
                length = lps[length - 1]
 
                # Also, note that we do not
                # increment i here
             
            else: # if (len == 0)
             
                lps[i] = 0
                i += 1
 
    return lps
 
# Method returns minimum character
# to be added at front to make
# string palindrome
def getMinCharToAddedToMakeStringPalin(string):
 
    revStr = string[::-1]
 
    # Get concatenation of string,
    # special character and reverse string
    concat = string + "$" + revStr
 
    # Get LPS array of this
    # concatenated string
    lps = computeLPSArray(concat)
 
    # By subtracting last entry of lps
    # vector from string length, we
    # will get our result
    return len(string) - lps[-1]
 
# Driver Code
if __name__ == "__main__":
 
    string = "AACECAAAA"
    print(getMinCharToAddedToMakeStringPalin(string))
     
# This code is contributed by Rituraj Jain


C#
// C# program for getting minimum character to be 
// added at front to make string palindrome 
using System;
using System.Text;
 
public class GFG{
 
  // returns vector lps for given string str 
  public static int[] computeLPSArray(string str)
  {
    int n = str.Length;
    int[] lps = new int[n];
    int i = 1, len = 0;
    lps[0] = 0; // lps[0] is always 0 
 
    while (i < n) 
    {
      if (str[i] == str[len])
      {
        len++;
        lps[i] = len;
        i++;
      }
      else
      {
        // This is tricky. Consider the example. 
        // AAACAAAA and i = 7. The idea is similar 
        // to search step.
        if (len != 0)
        {
          len = lps[len - 1];
 
          // Also, note that we do not increment 
          // i here 
        }
        else
        {
          lps[i] = 0;
          i++;
        }
      }
    }
    return lps;
  }
 
  // Method returns minimum character to be added at 
  // front to make string palindrome 
  static int getMinCharToAddedToMakeStringPalin(string str) 
  { 
    char[] s = str.ToCharArray();
 
    // Get concatenation of string, special character 
    // and reverse string 
    Array.Reverse( s );
    string rev = new string(s);
 
    string concat=  str + "$" + rev;
 
    // Get LPS array of this concatenated string
    int[] lps = computeLPSArray(concat);
    return str.Length - lps[concat.Length - 1];
  }
   
  // Driver Code
  static public void Main (){
    string str = "AACECAAAA"; 
    Console.WriteLine(getMinCharToAddedToMakeStringPalin(str));
  }
}
 
// This code is contributed by avanitrachhadiya2155


Javascript


输出:

2

感谢Sanny Kumar提出这种方法。
高效方法:我们可以使用 KMP 算法的 lps 数组在 O(n) 时间内有效地解决这个问题。
首先,我们通过连接给定字符串、特殊字符和给定字符串的反转来连接字符串,然后我们将得到这个连接字符串的 lps 数组,回想一下 lps 数组的每个索引代表最长的正确前缀,也是后缀。我们可以使用这个 lps 数组来解决这个问题。

For string = AACECAAAA
Concatenated String = AACECAAAA$AAAACECAA
LPS array will be {0, 1, 0, 0, 0, 1, 2, 2, 2, 
                   0, 1, 2, 2, 2, 3, 4, 5, 6, 7}

这里我们只对这个 lps 数组的最后一个值感兴趣,因为它向我们展示了与原始字符串的前缀匹配的反向字符串的最大后缀,即这些字符已经满足回文属性。最后,使字符串成为回文所需的最小字符数是输入字符串的长度减去 lps 数组的最后一个条目。请参阅下面的代码以更好地理解

C++

// C++ program for getting minimum character to be
// added at front to make string palindrome
#include 
using namespace std;
 
// returns vector lps for given string str
vector computeLPSArray(string str)
{
    int M = str.length();
    vector lps(M);
 
    int len = 0;
    lps[0] = 0; // lps[0] is always 0
 
    // the loop calculates lps[i] for i = 1 to M-1
    int i = 1;
    while (i < M)
    {
        if (str[i] == str[len])
        {
            len++;
            lps[i] = len;
            i++;
        }
        else // (str[i] != str[len])
        {
            // This is tricky. Consider the example.
            // AAACAAAA and i = 7. The idea is similar
            // to search step.
            if (len != 0)
            {
                len = lps[len-1];
 
                // Also, note that we do not increment
                // i here
            }
            else // if (len == 0)
            {
                lps[i] = 0;
                i++;
            }
        }
    }
    return lps;
}
 
// Method returns minimum character to be added at
// front to make string palindrome
int getMinCharToAddedToMakeStringPalin(string str)
{
    string revStr = str;
    reverse(revStr.begin(), revStr.end());
 
    // Get concatenation of string, special character
    // and reverse string
    string concat = str + "$" + revStr;
 
    //  Get LPS array of this concatenated string
    vector lps = computeLPSArray(concat);
 
    // By subtracting last entry of lps vector from
    // string length, we will get our result
    return (str.length() - lps.back());
}
 
// Driver program to test above functions
int main()
{
    string str = "AACECAAAA";
    cout << getMinCharToAddedToMakeStringPalin(str);
    return 0;
}

Java

// Java program for getting minimum character to be
// added at front to make string palindrome
import java.util.*;
class GFG
{
 
// returns vector lps for given string str
public static int[] computeLPSArray(String str)
{
    int n = str.length();
    int lps[] = new int[n];
    int i = 1, len = 0;
    lps[0] = 0; // lps[0] is always 0
     
    while (i < n)
    {
        if (str.charAt(i) == str.charAt(len))
        {
            len++;
            lps[i] = len;
            i++;
        }
        else
        {
            // This is tricky. Consider the example.
            // AAACAAAA and i = 7. The idea is similar
            // to search step.
            if (len != 0)
            {
                len = lps[len - 1];
                 
                // Also, note that we do not increment
                // i here
            }
            else
            {
                lps[i] = 0;
                i++;
            }
        }
    }
    return lps;
}
 
// Method returns minimum character to be added at
// front to make string palindrome
static int getMinCharToAddedToMakeStringPalin(String str)
{
    StringBuilder s = new StringBuilder();
    s.append(str);
     
    // Get concatenation of string, special character
    // and reverse string
    String rev = s.reverse().toString();
    s.reverse().append("$").append(rev);
     
    // Get LPS array of this concatenated string
    int lps[] = computeLPSArray(s.toString());
    return str.length() - lps[s.length() - 1];
}
 
// Driver Code
public static void main(String args[])
{
    String str = "AACECAAAA";
    System.out.println(getMinCharToAddedToMakeStringPalin(str));
}
}
 
// This code is contributed by Sparsh Singhal

Python3

# Python3 program for getting minimum
# character to be added at the front
# to make string palindrome
 
# Returns vector lps for given string str
def computeLPSArray(string):
 
    M = len(string)
    lps = [None] * M
 
    length = 0
    lps[0] = 0 # lps[0] is always 0
 
    # the loop calculates lps[i]
    # for i = 1 to M-1
    i = 1
    while i < M:
     
        if string[i] == string[length]:
         
            length += 1
            lps[i] = length
            i += 1
         
        else: # (str[i] != str[len])
         
            # This is tricky. Consider the example.
            # AAACAAAA and i = 7. The idea is
            # similar to search step.
            if length != 0:
             
                length = lps[length - 1]
 
                # Also, note that we do not
                # increment i here
             
            else: # if (len == 0)
             
                lps[i] = 0
                i += 1
 
    return lps
 
# Method returns minimum character
# to be added at front to make
# string palindrome
def getMinCharToAddedToMakeStringPalin(string):
 
    revStr = string[::-1]
 
    # Get concatenation of string,
    # special character and reverse string
    concat = string + "$" + revStr
 
    # Get LPS array of this
    # concatenated string
    lps = computeLPSArray(concat)
 
    # By subtracting last entry of lps
    # vector from string length, we
    # will get our result
    return len(string) - lps[-1]
 
# Driver Code
if __name__ == "__main__":
 
    string = "AACECAAAA"
    print(getMinCharToAddedToMakeStringPalin(string))
     
# This code is contributed by Rituraj Jain

C#

// C# program for getting minimum character to be 
// added at front to make string palindrome 
using System;
using System.Text;
 
public class GFG{
 
  // returns vector lps for given string str 
  public static int[] computeLPSArray(string str)
  {
    int n = str.Length;
    int[] lps = new int[n];
    int i = 1, len = 0;
    lps[0] = 0; // lps[0] is always 0 
 
    while (i < n) 
    {
      if (str[i] == str[len])
      {
        len++;
        lps[i] = len;
        i++;
      }
      else
      {
        // This is tricky. Consider the example. 
        // AAACAAAA and i = 7. The idea is similar 
        // to search step.
        if (len != 0)
        {
          len = lps[len - 1];
 
          // Also, note that we do not increment 
          // i here 
        }
        else
        {
          lps[i] = 0;
          i++;
        }
      }
    }
    return lps;
  }
 
  // Method returns minimum character to be added at 
  // front to make string palindrome 
  static int getMinCharToAddedToMakeStringPalin(string str) 
  { 
    char[] s = str.ToCharArray();
 
    // Get concatenation of string, special character 
    // and reverse string 
    Array.Reverse( s );
    string rev = new string(s);
 
    string concat=  str + "$" + rev;
 
    // Get LPS array of this concatenated string
    int[] lps = computeLPSArray(concat);
    return str.Length - lps[concat.Length - 1];
  }
   
  // Driver Code
  static public void Main (){
    string str = "AACECAAAA"; 
    Console.WriteLine(getMinCharToAddedToMakeStringPalin(str));
  }
}
 
// This code is contributed by avanitrachhadiya2155

Javascript


输出:

2