📌  相关文章
📜  给定字符串中长度为X的唯一回文字符串的计数

📅  最后修改于: 2021-06-25 12:46:40             🧑  作者: Mango

给定一个字符串s和一个整数X ,我们的任务是从给定的字符串找到长度为X的不同回文字符串的数量。

例子:

天真的方法:天真的方法是生成所有可能的长度为X的子序列,然后检查该子序列是否形成回文。

时间复杂度: O(2 N )
辅助空间: O(X)

高效的方法:想法是找到频率 字符串的所有字符的一个。计算可以放在特定位置的字符的不同数量,并将计数数量减少2,因为回文字符串的位置(i)和(X – i)相同。如果X的长度是奇数,那么我们必须在该位置X / 2处放置唯一的一个字符。

下面是上述方法的实现:

C++
// C++ implementation to count different
// palindromic string of length X
// from the given string S
 
#include 
using namespace std;
 
// Function to count different
// palindromic string of length X
// from the given string S
long long findways(string s, int x)
{
    // Base case
    if (x > (int)s.length())
        return 0;
 
    long long int n = (int)s.length();
 
    // Create the frequency array
    int freq[26];
 
    // Intitalise frequency array with 0
    memset(freq, 0, sizeof freq);
 
    // Count the frequency in the string
    for (int i = 0; i < n; ++i)
        freq[s[i] - 'a']++;
 
    multiset se;
 
    for (int i = 0; i < 26; ++i)
        if (freq[i] > 0)
            // Store frequency of the char
            se.insert(freq[i]);
 
    long long ans = 1;
 
    for (int i = 0; i < x / 2; ++i) {
        long long int count = 0;
        for (auto u : se) {
            // check the frequency which
            // is greater than zero
            if (u >= 2)
 
                // No. of different char we can
                // put at the position of
                // the i and x - i
                count++;
        }
 
        if (count == 0)
            return 0;
 
        else
            ans = ans * count;
 
        // Iterator pointing to the
        // last element of the set
        auto p = se.end();
        p--;
        int val = *p;
        se.erase(p);
        if (val > 2)
            // decrease the value of the char
            // we put on the position i and n - i
            se.insert(val - 2);
    }
 
    if (x % 2 != 0) {
        long long int count = 0;
        for (auto u : se)
            // different no of char we can
            // put at the position x/2
            if (u > 0)
                count++;
 
        ans = ans * count;
    }
 
    // Return total no of
    // different string
    return ans;
}
 
// Driver code
int main()
{
    string s = "aaa";
    int x = 2;
    cout << findways(s, x);
 
    return 0;
}


Java
// Java implementation to count different
// palindromic string of length X from the
// given string S
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
 
class GFG{
 
// Function to count different
// palindromic string of length X
// from the given string S
static int findways(String s, int x)
{
     
    // Base case
    if (x > s.length())
        return 0;
 
    int n = s.length();
 
    // Create the frequency array
    int[] freq = new int[26];
 
    // Intitalise frequency array with 0
    Arrays.fill(freq, 0);
 
    // Count the frequency in the string
    for(int i = 0; i < n; ++i)
        freq[s.charAt(i) - 'a']++;
 
    // multiset se;
    Set se = new HashSet<>();
 
    for(int i = 0; i < 26; ++i)
        if (freq[i] > 0)
         
            // Store frequency of the char
            se.add(freq[i]);
 
    int ans = 1;
 
    for(int i = 0; i < x / 2; ++i)
    {
        int count = 0;
        for(int u : se)
        {
             
            // Check the frequency which
            // is greater than zero
            if (u >= 2)
 
                // No. of different char we can
                // put at the position of
                // the i and x - i
                count++;
        }
 
        if (count == 0)
            return 0;
        else
            ans = ans * count;
 
        // Iterator pointing to the
        // last element of the set
        int p = (int)se.toArray()[se.size() - 1];
        int val = p;
        se.remove(p);
         
        if (val > 2)
         
            // Decrease the value of the char
            // we put on the position i and n - i
            se.add(val - 2);
    }
 
    if (x % 2 != 0)
    {
        int count = 0;
         
        for(int u : se)
         
            // Different no of char we can
            // put at the position x/2
            if (u > 0)
                count++;
 
        ans = ans * count;
    }
 
    // Return total no of
    // different string
    return ans;
}
 
// Driver code
public static void main(String[] args)
{
    String s = "aaa";
    int x = 2;
     
    System.out.println(findways(s, x));
}
}
 
// This code is contributed by sanjeev2552


Python3
# Python3 implementation to count
# different palindromic string of
# length X from the given string S
 
# Function to count different
# palindromic string of length X
# from the given string S
def findways(s, x):
 
    # Base case
    if(x > len(s)):
        return 0
 
    n = len(s)
 
    # Create the frequency array
    # Intitalise frequency array with 0
    freq = [0] * 26
 
    # Count the frequency in the string
    for i in range(n):
        freq[ord(s[i]) - ord('a')] += 1
 
    se = set()
 
    for i in range(26):
        if(freq[i] > 0):
             
            # Store frequency of the char
            se.add(freq[i])
 
    ans = 1
    for i in range(x // 2):
        count = 0
        for u in se:
             
            # Check the frequency which
            # is greater than zero
            if(u >= 2):
 
                # No. of different char we can
                # put at the position of
                # the i and x - i
                count += 1
 
        if(count == 0):
            return 0
        else:
            ans *= count
 
        # Iterator pointing to the
        # last element of the set
        p = list(se)
        val = p[-1]
        p.pop(-1)
        se = set(p)
 
        if(val > 2):
             
            # Decrease the value of the char
            # we put on the position i and n - i
            se.add(val - 2)
 
    if(x % 2 != 0):
        count = 0
        for u in se:
             
            # Different no of char we can
            # put at the position x/2
            if(u > 0):
                count += 1
 
        ans = ans * count
 
    # Return total no of
    # different string
    return ans
 
# Driver code
if __name__ == '__main__':
 
    s = "aaa"
    x = 2
     
    print(findways(s, x))
 
# This code is contributed by Shivam Singh


C#
// C# implementation to count different
// palindromic string of length X from the
// given string S
using System;
using System.Collections.Generic;
 
class GFG {
 
    // Function to count different
    // palindromic string of length X
    // from the given string S
    static int findways(string s, int x)
    {
 
        // Base case
        if (x > s.Length)
            return 0;
        int n = s.Length;
 
        // Create the frequency array
        int[] freq = new int[26];
 
        // Count the frequency in the string
        for (int i = 0; i < n; ++i)
            freq[s[i] - 'a']++;
 
        // multiset se;
        HashSet se = new HashSet();
        for (int i = 0; i < 26; ++i)
            if (freq[i] > 0)
 
                // Store frequency of the char
                se.Add(freq[i]);
 
        int ans = 1;
        for (int i = 0; i < x / 2; ++i)
        {
            int count = 0;
            foreach(int u in se)
            {
 
                // Check the frequency which
                // is greater than zero
                if (u >= 2)
 
                    // No. of different char we can
                    // put at the position of
                    // the i and x - i
                    count++;
            }
 
            if (count == 0)
                return 0;
            else
                ans = ans * count;
 
            // Iterator pointing to the
            // last element of the set
            int[] arr = new int[se.Count];
            se.CopyTo(arr);
            int p = arr[se.Count - 1];
            int val = p;
            se.Remove(p);
            if (val > 2)
 
                // Decrease the value of the char
                // we put on the position i and n - i
                se.Add(val - 2);
        }
 
        if (x % 2 != 0)
        {
            int count = 0;
 
            foreach(int u in se)
 
                // Different no of char we can
                // put at the position x/2
                if (u > 0) count++;
            ans = ans * count;
        }
 
        // Return total no of
        // different string
        return ans;
    }
 
  // Driver code
    static public void Main()
    {
        string s = "aaa";
        int x = 2;
        Console.WriteLine(findways(s, x));
    }
}
 
// This code is contributed by dharanendralv23


输出:
1

时间复杂度: O(N + 26 * X)