📌  相关文章
📜  通过交换一对字符可以计算回文字符串

📅  最后修改于: 2021-05-07 00:36:27             🧑  作者: Mango

给定回文字符串S ,任务是通过一次交换一对字符来查找回文字符串的数量。
例子:

天真的方法:
解决该问题的最简单方法是从给定的字符串生成所有可能的字符对,如果每对字符交换,它们是否会生成回文字符串。如果发现是真的,请增加count 。最后,打印count的值。

时间复杂度: O(N 3 )
辅助空间: O(1)

高效方法:
为了优化上述方法,请计算字符串中每个字符的频率。为了使字符串保持回文,只能在字符串交换相同的字符。
请按照以下步骤解决问题:

  • 遍历字符串。
  • 对于每个i字符,请以该字符的当前频率增加计数。这增加了当前字符与其之前出现的交换次数。
  • 增加第i字符的频率。
  • 最后,在完全遍历字符串,打印count

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
#include 
using namespace std;
  
// Function to return the count of
// possible palindromic strings
long long findNewString(string s)
{
  
    long long ans = 0;
  
    // Stores the frequencies
    // of each character
    int freq[26];
  
    // Stores the length of
    // the string
    int n = s.length();
  
    // Initialize frequencies
    memset(freq, 0, sizeof freq);
  
    for (int i = 0; i < (int)s.length(); ++i) {
  
        // Increase the number of swaps,
        // the current character make with
        // its previous occurrences
        ans += freq[s[i] - 'a'];
  
        // Increase frequency
        freq[s[i] - 'a']++;
    }
  
    return ans;
}
  
// Driver Code
int main()
{
    string s = "aaabaaa";
    cout << findNewString(s) << '\n';
  
    return 0;
}


Java
// Java Program to implement
// the above approach
import java.util.*;
class GFG{
  
// Function to return the count of
// possible palindromic Strings
static long findNewString(String s)
{
    long ans = 0;
  
    // Stores the frequencies
    // of each character
    int []freq = new int[26];
  
    // Stores the length of
    // the String
    int n = s.length();
  
    // Initialize frequencies
    Arrays.fill(freq, 0);
  
    for (int i = 0; i < (int)s.length(); ++i)
    {
  
        // Increase the number of swaps,
        // the current character make with
        // its previous occurrences
        ans += freq[s.charAt(i) - 'a'];
  
        // Increase frequency
        freq[s.charAt(i) - 'a']++;
    }
    return ans;
}
  
// Driver Code
public static void main(String[] args)
{
    String s = "aaabaaa";
    System.out.print(findNewString(s));
}
}
  
// This code is contributed by sapnasingh4991


Python3
# Python3 program to implement
# the above approach
  
# Function to return the count of
# possible palindromic strings
def findNewString(s):
  
    ans = 0
  
    # Stores the frequencies
    # of each character
    freq = [0] * 26
  
    # Stores the length of
    # the string
    n = len(s)
  
    for i in range(n):
  
        # Increase the number of swaps,
        # the current character make with
        # its previous occurrences
        ans += freq[ord(s[i]) - ord('a')]
  
        # Increase frequency
        freq[ord(s[i]) - ord('a')] += 1
      
    return ans
  
# Driver Code
s = "aaabaaa"
  
print(findNewString(s))
  
# This code is contributed by code_hunt


C#
// C# Program to implement
// the above approach
using System;
class GFG{
  
// Function to return the count of
// possible palindromic Strings
static long findNewString(String s)
{
    long ans = 0;
  
    // Stores the frequencies
    // of each character
    int []freq = new int[26];
  
    // Stores the length of
    // the String
    int n = s.Length;
  
    for (int i = 0; i < (int)s.Length; ++i)
    {
  
        // Increase the number of swaps,
        // the current character make with
        // its previous occurrences
        ans += freq[s[i] - 'a'];
  
        // Increase frequency
        freq[s[i] - 'a']++;
    }
    return ans;
}
  
// Driver Code
public static void Main(String[] args)
{
    String s = "aaabaaa";
    Console.Write(findNewString(s));
}
}
  
// This code is contributed by sapnasingh4991


输出:
15

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