给定一个由N个字符串组成的数组A [] ,任务是计算在合并后形成回文字符串或可以重新排列以形成回文字符串的可能字符串的对数。
例子 :
Input: N = 6, A[ ] = {aab, abcac, dffe, ed, aa, aade}
Output: 6
Explanation:
All possible pairs of strings which generates a palindromic string:
{aab, abcac} = aabccbaa
{aab, aa} = aabaa
{abcac, aa} = aacbcaa
{dffe, ed} = fdeedf
{dffe, aade} = edfaafde
{ed, aade} = edaade or aeddea
Hence, total possible pairs = 6
Input: N = 3, A[ ] = {aa, bb, cd}
Output: 1
Explanation:
The only possible pair of strings which generates palindromic string:
{aa, bb} = abba
Hence, total possible pairs = 1
天真的方法:
解决此问题的最简单方法是从给定数组生成所有可能的对,并将这些对合并以生成新的字符串。对于每个合并的字符串,生成该字符串的所有可能排列,对于每个排列,检查它是否是回文字符串,并相应地增加计数。
时间复杂度: O(N 2 * L!* L),其中L是字符串的最大长度。
辅助空间: O(M)
高效方法:
当一个字符最多出现一个奇数而其余每个字符都出现一个偶数时,回文字符串总是可以形成的。由于允许重新排列合并字符串中的字符,因此我们只需要计算合并字符串中字符的频率,并检查其是否满足回文字符串。
Illustration:
1. Every character has even occurrence
Strings “aab” and “abcac” can be merged and rearranged to form a palindromic string “aababcac” with each character having even frequency
2. One character has odd occurrence
Strings “aab” and “aa” can be merged to form a palindromic string “aabaa” with a character(‘b’) having odd frequency and the rest having even frequency.
因此,可以观察到可以将两对以下类型的字符串合并成回文字符串:
- 两个字符串中每个字符的频率相同。
- 两个字符串最多一个字符具有不同的频率。
请按照以下步骤解决问题:
- 遍历给定的数组,并为每个字符串存储每个字符的频率。
- 为每个频率分配奇偶校验(0或1) 。将0分配给偶数频率,将1分配给奇数频率。
- 初始化映射以存储每个生成的频率阵列的频率。
- 由于允许重新排列,因此请反转每个字符的奇偶校验并将频率数组包括在映射中。
- 最后,返回相似频率阵列的总数,这将给出所需的对数。
下面是上述方法的实现:
C++
// C++ Program to find
// palindromic string
#include
using namespace std;
int getCount(int N, vector& s)
{
// Stores frequency array
// and its count
map, int> mp;
// Total number of pairs
int ans = 0;
for (int i = 0; i < N; i++) {
// Initializing array of size 26
// to store count of character
vector a(26, 0);
// Counting occurrence of each
// character of current string
for (int j = 0; j < s[i].size(); j++) {
a[s[i][j] - 'a']++;
}
// Convert each count to parity(0 or 1)
// on the basis of its frequency
for (int j = 0; j < 26; j++) {
a[j] = a[j] % 2;
}
// Adding to anser
ans += mp[a];
// Frequency of single character
// can be possibly changed,
// so change its parity
for (int j = 0; j < 26; j++) {
vector changedCount = a;
if (a[j] == 0)
changedCount[j] = 1;
else
changedCount[j] = 0;
ans += mp[changedCount];
}
mp[a]++;
}
return ans;
}
int main()
{
int N = 6;
vector A
= { "aab", "abcac", "dffe",
"ed", "aa", "aade" };
cout << getCount(N, A);
return 0;
}
Java
// Java program to find
// palindromic string
import java.util.*;
class GFG{
static int getCount(int N, List s)
{
// Stores frequency array
// and its count
Map, Integer> mp = new HashMap<>();
// Total number of pairs
int ans = 0;
for(int i = 0; i < N; i++)
{
// Initializing array of size 26
// to store count of character
List a = new ArrayList<>(26);
for(int k = 0; k < 26; k++)
a.add(0);
// Counting occurrence of each
// character of current string
for(int j = 0; j < s.get(i).length(); j++)
{
a.set((s.get(i).charAt(j) - 'a'),
a.get(s.get(i).charAt(j) - 'a') + 1);
}
// Convert each count to parity(0 or 1)
// on the basis of its frequency
for(int j = 0; j < 26; j++)
{
a.set(j, a.get(j) % 2);
}
// Adding to anser
ans += mp.getOrDefault(a, 0);
// Frequency of single character
// can be possibly changed,
// so change its parity
for(int j = 0; j < 26; j++)
{
List changedCount = new ArrayList<>();
changedCount.addAll(a);
if (a.get(j) == 0)
changedCount.set(j, 1);
else
changedCount.set(j, 0);
ans += mp.getOrDefault(changedCount, 0);
}
mp.put(a, mp.getOrDefault(a, 0) + 1);
}
return ans;
}
// Driver code
public static void main (String[] args)
{
int N = 6;
List A = Arrays.asList("aab", "abcac",
"dffe", "ed",
"aa", "aade");
System.out.print(getCount(N, A));
}
}
// This code is contributed by offbeat
Python3
# Python3 program to find
# palindromic string
from collections import defaultdict
def getCount(N, s):
# Stores frequency array
# and its count
mp = defaultdict(lambda: 0)
# Total number of pairs
ans = 0
for i in range(N):
# Initializing array of size 26
# to store count of character
a = [0] * 26
# Counting occurrence of each
# character of current string
for j in range(len(s[i])):
a[ord(s[i][j]) - ord('a')] += 1
# Convert each count to parity(0 or 1)
# on the basis of its frequency
for j in range(26):
a[j] = a[j] % 2
# Adding to anser
ans += mp[tuple(a)]
# Frequency of single character
# can be possibly changed,
# so change its parity
for j in range(26):
changedCount = a[:]
if (a[j] == 0):
changedCount[j] = 1
else:
changedCount[j] = 0
ans += mp[tuple(changedCount)]
mp[tuple(a)] += 1
return ans
# Driver code
if __name__ == '__main__':
N = 6
A = [ "aab", "abcac", "dffe",
"ed", "aa", "aade" ]
print(getCount(N, A))
# This code is contributed by Shivam Singh
6
时间复杂度: O(N *(L + log(N))),其中L是字符串的最大长度
辅助空间: O(N)