📌  相关文章
📜  可以插入字母以使字符串变为回文的位置数

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

可以插入字母以使字符串变为回文的位置数

给定一个字符串str ,我们需要找到编号。可以插入字母(小写)以使字符串变为回文的位置。
例子:

Input : str = "abca"
Output : possible palindromic strings: 
         1) acbca (at position 2)
         2) abcba (at position 4)
         Hence, the output is 2.

Input : str = "aaa"
Output : possible palindromic strings:
         1) aaaa
         2) aaaa
         3) aaaa
         4) aaaa
         Hence, the output is 4. 

朴素方法:这种方法是在每个可能的位置(即 N+1 个位置)插入所有 26 个字母,并在每个位置检查该插入是否使其成为回文并增加计数。
有效的方法:
首先,您必须观察到我们必须仅在该点处的字符违反回文条件时才进行插入,即, S[i] != S[N-i-1]  .现在,基于上述事实,将有两种情况:
案例一:如果给定的字符串已经是回文怎么办
然后我们只能在插入不违反回文的位置插入。
1)如果长度是偶数,那么我们总是可以在中间插入任何字母。
2)如果长度是奇数,那么我们可以将中间的字母插入到它的左侧或右侧。
3)在这两种情况下,我们都可以插入中间的字母(让它成为'CH' ),位置等于:
(中间字母左侧的连续 CH 数量)*2
案例二:如果不是回文
如上所述,我们应该在位置开始插入S[i] != S[N-1-i]  , 所以我们增加计数并检查是否在任何其他位置插入使其成为回文。
1) 如果S[i]...S[N-i-2]  是回文,那么我们可以在之前的任意位置插入* S[i]  直到S[K] != S[N-i-1]  , K在范围内[i-1, 0]  .(*字母 = S[Ni-1])
2.)如果S[i+1]...S[N-i-1]  是回文,那么我们可以在后面的任意位置插入* S[n-i-1]  直到S[K] != S[i]  , K在范围内[N-i, N-1]  .(*字母 = S[i])
在所有情况下,我们都会不断增加计数。

C++
// CPP code to find the no.of positions where a
// letter can be inserted to make it a palindrome
#include 
using namespace std;
 
// Function to check if the string is palindrome
bool isPalindrome(string &s, int i, int j)
{
    int p = j;
    for (int k = i; k <= p; k++) {
        if (s[k] != s[p])
            return false;
        p--;
    }
    return true;
}
 
int countWays(string &s)
{
    // to know the length of string
    int n = s.length();
    int count = 0;
 
    // if the given string is a palindrome(Case-I)
    if (isPalindrome(s, 0, n - 1))
    {
        // Sub-case-III)
        for (int i = n / 2; i < n; i++)
        {
            if (s[i] == s[i + 1])
                count++;
            else
                break;
        }
        if (n % 2 == 0) // if the length is even
        {
            count++;
            count = 2 * count + 1; // sub-case-I
        } else
            count = 2 * count + 2; // sub-case-II
    } else {
        for (int i = 0; i < n / 2; i++) {
 
            // insertion point
            if (s[i] != s[n - 1 - i])
            {
                int j = n - 1 - i;
 
                // Case-I
                if (isPalindrome(s, i, n - 2 - i))
                {
                    for (int k = i - 1; k >= 0; k--) {
                        if (s[k] != s[j])
                            break;
                        count++;
                    }
                    count++;
                }
 
                // Case-II
                if (isPalindrome(s, i + 1, n - 1 - i))
                {
                    for (int k = n - i; k < n; k++) {
                        if (s[k] != s[i])
                            break;
                        count++;
                    }
                    count++;
                }
                break;
            }
        }
    }
     
    return count;
}
 
// Driver code
int main()
{
    string s = "abca";
    cout << countWays(s) << endl;
    return 0;
}


Java
// Java code to find the no.of positions where a
// letter can be inserted to make it a palindrome
 
import java.io.*;
 
class GFG {
     
    // Function to check if the string is palindrome
    static boolean isPalindrome(String s, int i, int j)
    {
        int p = j;
        for (int k = i; k <= p; k++) {
            if (s.charAt(k) != s.charAt(p))
                return false;
            p--;
        }
         
        return true;
    }
 
    static int countWays(String s)
    {
         
        // to know the length of string
        int n = s.length();
        int count = 0;
 
        // if the given string is a palindrome(Case-I)
        if (isPalindrome(s, 0, n - 1)) {
             
            // Sub-case-III)
            for (int i = n / 2; i < n; i++) {
                if (s.charAt(i) == s.charAt(i + 1))
                    count++;
                else
                    break;
            }
             
            if (n % 2 == 0) // if the length is even
            {
                count++;
                count = 2 * count + 1; // sub-case-I
            }
            else
                count = 2 * count + 2; // sub-case-II
        }
        else {
            for (int i = 0; i < n / 2; i++) {
 
                // insertion point
                if (s.charAt(i) != s.charAt(n - 1 - i)) {
                    int j = n - 1 - i;
 
                    // Case-I
                    if (isPalindrome(s, i, n - 2 - i)) {
                        for (int k = i - 1; k >= 0; k--) {
                            if (s.charAt(k) != s.charAt(j))
                                break;
                            count++;
                        }
                        count++;
                    }
 
                    // Case-II
                    if (isPalindrome(s, i + 1, n - 1 - i)) {
                        for (int k = n - i; k < n; k++) {
                            if (s.charAt(k) != s.charAt(i))
                                break;
                            count++;
                        }
                        count++;
                    }
                    break;
                }
            }
        }
 
        return count;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        String s = "abca";
        System.out.println(countWays(s));
    }
}
 
// This code is contributed by vt_m.


Python 3
# Python 3 code to find the no.of positions
# where a letter can be inserted to make it
# a palindrome
 
# Function to check if the string
# is palindrome
def isPalindrome(s, i, j):
 
    p = j
    for k in range(i, p + 1):
        if (s[k] != s[p]):
            return False
        p -= 1
     
    return True
 
def countWays(s):
 
    # to know the length of string
    n = len(s)
    count = 0
 
    # if the given string is a palindrome(Case-I)
    if (isPalindrome(s, 0, n - 1)) :
     
        # Sub-case-III)
        for i in range(n // 2, n):
 
            if (s[i] == s[i + 1]):
                count += 1
            else:
                break
         
        if (n % 2 == 0): # if the length is even
            count += 1
            count = 2 * count + 1 # sub-case-I
        else:
            count = 2 * count + 2 # sub-case-II
    else :
        for i in range(n // 2) :
 
            # insertion point
            if (s[i] != s[n - 1 - i]) :
                j = n - 1 - i
 
                # Case-I
                if (isPalindrome(s, i, n - 2 - i)) :
                    for k in range(i - 1, -1, -1):
                        if (s[k] != s[j]):
                            break
                        count += 1
                     
                    count += 1
 
                # Case-II
                if (isPalindrome(s, i + 1, n - 1 - i)) :
                    for k in range(n - i, n) :
                        if (s[k] != s[i]):
                            break
                        count += 1
                     
                    count += 1
                 
                break
     
    return count
 
# Driver code
if __name__ == "__main__":
     
    s = "abca"
    print(countWays(s))
 
# This code is contributed by ita_c


C#
// C# code to find the no. of positions
// where a letter can be inserted
// to make it a palindrome.
using System;
 
class GFG {
     
    // Function to check if the
    // string is palindrome
    static bool isPalindrome(String s, int i,
                                       int j)
    {
        int p = j;
        for (int k = i; k <= p; k++)
        {
            if (s[k] != s[p])
                return false;
            p--;
        }
         
        return true;
    }
 
    static int countWays(String s)
    {
         
        // to know the length of string
        int n = s.Length;
        int count = 0;
 
        // if the given string is
        // a palindrome(Case-I)
        if (isPalindrome(s, 0, n - 1)) {
             
            // Sub-case-III)
            for (int i = n / 2; i < n; i++) {
                if (s[i] == s[i + 1])
                    count++;
                else
                    break;
            }
             
            // if the length is even
            if (n % 2 == 0)
            {
                count++;
                 
                // sub-case-I
                count = 2 * count + 1;
            }
            else
             
                // sub-case-II
                count = 2 * count + 2;
        }
        else {
            for (int i = 0; i < n / 2; i++) {
 
                // insertion point
                if (s[i] != s[n - 1 - i]) {
                    int j = n - 1 - i;
 
                    // Case-I
                    if (isPalindrome(s, i, n - 2 - i)) {
                        for (int k = i - 1; k >= 0; k--) {
                            if (s[k] != s[j])
                                break;
                            count++;
                        }
                        count++;
                    }
 
                    // Case-II
                    if (isPalindrome(s, i + 1, n - 1 - i)) {
                        for (int k = n - i; k < n; k++) {
                            if (s[k] != s[i])
                                break;
                            count++;
                        }
                        count++;
                    }
                    break;
                }
            }
        }
 
        return count;
    }
 
    // Driver code
    public static void Main()
    {
        String s = "abca";
        Console.Write(countWays(s));
    }
}
 
// This code is contributed by nitin mittal


PHP
= 0; $k--)
                    {
                        if ($s[$k] != $s[$j])
                            break;
                        $count++;
                    }
                    $count++;
                }
 
                // Case-II
                if (isPalindrome($s, $i + 1,$n - 1 - $i))
                {
                    for ($k = $n - $i; $k < $n; $k++)
                    {
                        if ($s[$k] != $s[$i])
                            break;
                        $count++;
                    }
                    $count++;
                }
                break;
            }
        }
    }
     
    return $count;
}
 
// Driver code
$s = "abca";
echo countWays($s) ;
 
// This code is contributed by nitin mittal
?>


Javascript


输出:

2