📌  相关文章
📜  通过交换一对字符可以实现回文字符串的计数

📅  最后修改于: 2021-10-26 02:38:12             🧑  作者: Mango

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

天真的方法:
解决这个问题的最简单的方法是从给定的字符串生成所有可能的字符对,如果交换它们是否会生成回文字符串,那么对于每一对字符。如果发现是真的,增加计数。最后,打印count的值。
时间复杂度: O(N 3 )
辅助空间: O(1)
有效的方法:
为了优化上述方法,计算字符串中每个字符的频率。为了使字符串保持回文,只能在字符串交换相同的字符。
请按照以下步骤解决问题:

  • 遍历字符串。
  • 对于每一个字符,增加与字符的当前频率。这会增加当前角色与其先前出现的字符可以进行的交换次数。
  • 增加第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


Javascript


输出:

15

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程