用交替位置的辅音和元音计算字符串

给一根绳子 str 。任务是通过在给定字符串中用字母替换“$”来找到所有可能的字符串数。 笔记 :字母表的位置应确保字符串的元音和辅音始终交替,并且字符串必须始终以辅音开头。假设这样的字符串总是可能的,即不需要关心“$”以外的字符。 例子 :

null
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$” 在这里,我们必须找到使用给定约束形成字符串的多种方法。

  • $第一次出现在第二个位置,即第一个索引,因此我们可以使用5个元音。
  • $第二次出现在第三位,所以我们可以使用21个辅音。
  • $第三次出现在第五位,所以我们可以使用21个辅音。

所以,形成上述字符串的方法总数是 = 5*21*21 = 2205

以下是上述方法的实施情况:

C++

// C++ implementation of above approach
#include <bits/stdc++.h>
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

<?php
// PHP implementation of above approach
// Function to find the count of strings
function countStrings( $s )
{
// Variable to store the
// final result
$sum = 1;
// Loop iterating through string
for ( $i = 0; $i < strlen ( $s ); $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
// Let the string 'str' be s$$e$
$str = "s$$e$" ;
// Print result
echo countStrings( $str );
// This code is contributed by Ryuga
?>


Javascript

<script>
// javascript implementation of above approach
// Function to find the count of strings
function countStrings( s)
{
// Variable to store the final result
let sum = 1;
// Loop iterating through string
for (let 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
// Let the string 'str' be s$$e$
let str = "s$$e$" ;
// Print result
document.write(countStrings(str));
// This code is contributed by gauravrajput1
</script>


输出:

2205

© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享