# Count strings with consonants and vowels at alternate position

Given a string **str**. The task is to find all possible number of strings that can be obtained by replacing the “$” with alphabets in the given string.

**Note**: Alphabets should be placed in such a way that the string is always alternating in vowels and consonants, and the string must always start with a consonant. It is assumed that such a string is always possible, i.e. there is no need to care about the characters other than “$”.

**Examples**:

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

**Approach:** It is given that the string will start with a consonant. So, if ‘$’ is at even position(considering 0-based indexing) then there should be a consonant else there should be a vowel. Also, given that there is no need to care about the characters other than “$”, i.e., characters other than “$” are placed correctly in the string maintaining the alternating consonant and vowel sequence. Let us understand the problem with an example.

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

Below is the implementation of the above approach:

## 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; ` `} ` |

*chevron_right*

*filter_none*

## 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)); ` `} ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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)); ` `} ` `} ` |

*chevron_right*

*filter_none*

## 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 ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

2205

## Recommended Posts:

- Find the number of words of X vowels and Y consonants that can be formed from M vowels and N consonants
- Modify string by removing vowels in between two consonants
- Replace all consonants with nearest vowels in a string
- Number of words that can be made using exactly P consonants and Q vowels from the given string
- Modify the string by swapping continuous vowels or consonants
- Arrange consonants and vowels nodes in a linked list
- Check whether all the substrings have number of vowels atleast as that of consonants
- Count of pairs of strings which differ in exactly one position
- Encrypt string with product of number of vowels and consonants in substring of size k
- Arrangement of words without changing the relative position of vowel and consonants
- Check if a string can be converted to another string by replacing vowels and consonants
- Count number of rotated strings which have more number of vowels in the first half than second half
- Count consonants in a string (Iterative and recursive methods)
- Replace consonants with next immediate consonants alphabetically in a String
- Count substrings that contain all vowels | SET 2

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.