给定一个字符串str 。任务是找到所有可能的字符串数,这些字符串可以通过用给定字符串的字母替换“ $”来获得。
注意:字母的放置方式应使字符串在元音和辅音中始终交替出现,并且字符串必须始终以辅音开头。假定这样的字符串总是可能的,即,除了“ $”以外,不需要关心其他字符。
例子:
Input: str = "y$s"
Output: 5
$ can be replaced with any of the 5 vowels.
So, there can be 5 strings.
Input: str = "s$$e$"
Output: 2205
方法:假定字符串将以辅音开头。因此,如果“ $”处于偶数位置(考虑从0开始的索引),则应该有辅音,否则应有元音。另外,由于不需要关心“ $”以外的字符,即,将“ $”以外的字符正确地放置在保持交替的辅音和元音序列的字符串。让我们用一个例子来理解这个问题。
str = “s$$e$”
Here we have to find the number of ways to form a string with given constraints.
- First occurrence of $ is at 2nd position i.e. 1st index, so we can use 5 vowels.
- Second occurrence of $ is at 3rd position, so we can use 21 consonants.
- Third occurrence of $ is at 5th position, so we can use 21 consonants.
So, total number of ways to form above string is = 5*21*21 = 2205
下面是上述方法的实现:
C++
// C++ implementation of above approach
#include
using namespace std;
// Function to find the count of strings
int countStrings(string s)
{
// Variable to store the final result
long sum = 1;
// Loop iterating through string
for (int i = 0; i < s.size(); i++) {
// If '$' is present at the even
// position in the string
if (i % 2 == 0 && s[i] == '$')
//'sum' is multiplied by 21
sum *= 21;
// If '$' is present at the odd
// position in the string
else if (s[i] == '$')
//'sum' is multiplied by 5
sum *= 5;
}
return sum;
}
// Driver code
int main()
{
// Let the string 'str' be s$$e$
string str = "s$$e$";
// Print result
cout << countStrings(str) << endl;
return 0;
}
Java
// Java implementation of above approach
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG{
// Function to find the count of strings
static int countStrings(String s)
{
// Variable to store the final result
int sum = 1;
// Loop iterating through string
for (int i = 0; i < s.length(); i++) {
// If '$' is present at the even
// position in the string
if (i % 2 == 0 && s.charAt(i) == '$')
//'sum' is multiplied by 21
sum *= 21;
// If '$' is present at the odd
// position in the string
else if (s.charAt(i) == '$')
//'sum' is multiplied by 5
sum *= 5;
}
return sum;
}
// Driver code
public static void main(String args[])
{
// Let the string 'str' be s$$e$
String str = "s$$e$";
// Print result
System.out.println(countStrings(str));
}
}
Python 3
# Python 3 implementation of above approach
# Function to find the count of strings
def countStrings(s):
# Variable to store the final result
sum = 1
# Loop iterating through string
for i in range(len(s)):
# If '$' is present at the even
# position in the string
if (i % 2 == 0 and s[i] == '$'):
#'sum' is multiplied by 21
sum *= 21
# If '$' is present at the odd
# position in the string
elif(s[i] == '$'):
# 'sum' is multiplied by 5
sum *= 5
return sum
# Driver code
if __name__ == "__main__":
# Let the string 'str' be s$$e$
str = "s$$e$"
# Print result
print(countStrings(str))
# this code is contributed by ChitraNayal
C#
// C# implementation of above approach
using System;
class GFG{
// Function to find the count of strings
static int countStrings(String s)
{
// Variable to store the final result
int sum = 1;
// Loop iterating through string
for (int i = 0; i < s.Length; i++) {
// If '$' is present at the even
// position in the string
if (i % 2 == 0 && s[i] == '$')
//'sum' is multiplied by 21
sum *= 21;
// If '$' is present at the odd
// position in the string
else if (s[i] == '$')
//'sum' is multiplied by 5
sum *= 5;
}
return sum;
}
// Driver code
public static void Main()
{
// Let the string 'str' be s$$e$
String str = "s$$e$";
// Print result
Console.WriteLine(countStrings(str));
}
}
PHP
Javascript
输出:
2205
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。