给定长度为N的字符串S ,任务是计算第一个字符是辅音且没有一对辅音或元音彼此相邻的S的字谜数目。
例子:
Input: S = “GADO”
Output: 4
Explanation:
The anagrams of string S satisfying the given conditions are GADO, GODA, DOGA, DAGO.
Therefore, the total number of such anagrams is 4.
Input: S = “AABCY”
Output: 6
Explanation:
The anagrams of the string S satisfying the given conditions are BACAY, BAYAC, CABAY, CAYAB, YABAC, YACAB.
Therefore, the total number of such anagrams is 6.
天真的方法:最简单的方法是生成给定字符串的所有可能的字谜,并对满足给定条件的那些字谜进行计数。最后,打印获得的计数。
时间复杂度: O(N!* N)
辅助空间: O(1)
高效的方法:还可以基于以下观察来优化上述方法:
- 辅音和元音数目相等的字符串满足给定条件。
- 辅音比元音多一个的弦也满足给定条件。
- 除了这两个条件外,可能的字谜的计数始终为0 。
- 现在,可以通过使用组合公式来解决问题。考虑字符串S和C中有C 1 ,C 2 …,C N辅音和V 1 ,V 2 ,…,V N元音和\ sum C 分别表示辅音和元音的总数,则答案将是:
where,
Ci is the count of ith consonant.
Vi is the count of ith vowel.
请按照以下步骤解决问题:
- 初始化一个变量,例如answer ,以存储字谜的总数。
- 将字符串S的每个字符的频率存储在HashMap计数中。
- 将元音和辅音的数量分别存储在变量V和C中的S中。
- 如果V的值不等于C或C的值不等于(V +1) ,则打印0 。否则,请执行以下步骤:
- 将分母初始化为1 。
- 使用变量i遍历字符串S ,并将分母更新为分母*((count [S [i]])!) 。
- 将分子初始化为V!* C! ,并将answer的值更新为分子/分母。
- 完成上述步骤后,打印答案的值作为结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
#define ll long long
#define mod 1000000007
#define N 1000001
using namespace std;
// Function to compute factorials till N
void Precomputefact(unordered_map& fac)
{
ll ans = 1;
// Iterate in the range [1, N]
for (ll i = 1; i <= N; i++) {
// Update ans to ans*i
ans = (ans * i) % mod;
// Store the value of ans
// in fac[i]
fac[i] = ans;
}
return;
}
// Function to check whether the
// current character is a vowel or not
bool isVowel(char a)
{
if (a == 'A' || a == 'E' || a == 'I' || a == 'O'
|| a == 'U')
return true;
else
return false;
}
// Function to count the number of
// anagrams of S satisfying the
// given condition
void countAnagrams(string s, int n)
{
// Store the factorials upto N
unordered_map fac;
// Function Call to generate
// all factorials upto n
Precomputefact(fac);
// Create a hashmap to store
// frequencies of all characters
unordered_map count;
// Store the count of
// vowels and consonants
int vo = 0, co = 0;
// Iterate through all
// characters in the string
for (int i = 0; i < n; i++) {
// Update the frequency
// of current character
count[s[i]]++;
// Check if the character
// is vowel or consonant
if (isVowel(s[i]))
vo++;
else
co++;
}
// Check if ΣC==ΣV+1 or ΣC==ΣV
if ((co == vo + 1) || (co == vo)) {
// Store the denominator
ll deno = 1;
// Calculate the denominator
// of the expression
for (auto c : count) {
// Multiply denominator by factorial
// of counts of all letters
deno = (deno * fac[c.second]) % mod;
}
// Store the numerator
ll nume = fac[co] % mod;
nume = (nume * fac[vo]) % mod;
// Store the answer by dividing
// numerator by denominator
ll ans = nume / deno;
// Print the answer
cout << ans;
}
// Otherwise, print 0
else {
cout << 0;
}
}
// Driver Code
int main()
{
string S = "GADO";
int l = S.size();
countAnagrams(S, l);
return 0;
}
Python3
# Python 3 program for the above approach
#include
mod = 1000000007
N = 1000001
fac = {}
# Function to compute factorials till N
def Precomputefact():
global fac
ans = 1
# Iterate in the range [1, N]
for i in range(1,N+1,1):
# Update ans to ans*i
ans = (ans * i) % mod
# Store the value of ans
# in fac[i]
fac[i] = ans
return
# Function to check whether the
# current character is a vowel or not
def isVowel(a):
if (a == 'A' or a == 'E' or a == 'I' or a == 'O' or a == 'U'):
return True
else:
return False
# Function to count the number of
# anagrams of S satisfying the
# given condition
def countAnagrams(s,n):
# Store the factorials upto N
global fac
# Function Call to generate
# all factorials upto n
Precomputefact()
# Create a hashmap to store
# frequencies of all characters
count = {}
# Store the count of
# vowels and consonants
vo = 0
co = 0
# Iterate through all
# characters in the string
for i in range(n):
# Update the frequency
# of current character
if s[i] in count:
count[s[i]] += 1
else:
count[s[i]] = 1
# Check if the character
# is vowel or consonant
if (isVowel(s[i])):
vo += 1
else:
co += 1
# Check if ΣC==ΣV+1 or ΣC==ΣV
if ((co == vo + 1) or (co == vo)):
# Store the denominator
deno = 1
# Calculate the denominator
# of the expression
for key,value in count.items():
# Multiply denominator by factorial
# of counts of all letters
deno = (deno * fac[value]) % mod
# Store the numerator
nume = fac[co] % mod
nume = (nume * fac[vo]) % mod
# Store the answer by dividing
# numerator by denominator
ans = nume // deno
# Print the answer
print(ans)
# Otherwise, print 0
else:
print(0)
# Driver Code
if __name__ == '__main__':
S = "GADO"
l = len(S)
countAnagrams(S, l)
# This code is contributed by ipg2016107.
4
时间复杂度: O(N)
辅助空间: O(N)