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++
#include <bits/stdc++.h>
using namespace std;
int countStrings(string s)
{
long sum = 1;
for ( int i = 0; i < s.size(); i++) {
if (i % 2 == 0 && s[i] == '$' )
sum *= 21;
else if (s[i] == '$' )
sum *= 5;
}
return sum;
}
int main()
{
string str = "s$$e$" ;
cout << countStrings(str) << endl;
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG{
static int countStrings(String s)
{
int sum = 1 ;
for ( int i = 0 ; i < s.length(); i++) {
if (i % 2 == 0 && s.charAt(i) == '$' )
sum *= 21 ;
else if (s.charAt(i) == '$' )
sum *= 5 ;
}
return sum;
}
public static void main(String args[])
{
String str = "s$$e$" ;
System.out.println(countStrings(str));
}
}
|
Python 3
def countStrings(s):
sum = 1
for i in range ( len (s)):
if (i % 2 = = 0 and s[i] = = '$' ):
sum * = 21
elif (s[i] = = '$' ):
sum * = 5
return sum
if __name__ = = "__main__" :
str = "s$$e$"
print (countStrings( str ))
|
C#
using System;
class GFG{
static int countStrings(String s)
{
int sum = 1;
for ( int i = 0; i < s.Length; i++) {
if (i % 2 == 0 && s[i] == '$' )
sum *= 21;
else if (s[i] == '$' )
sum *= 5;
}
return sum;
}
public static void Main()
{
String str = "s$$e$" ;
Console.WriteLine(countStrings(str));
}
}
|
PHP
<?php
function countStrings( $s )
{
$sum = 1;
for ( $i = 0; $i < strlen ( $s ); $i ++)
{
if ( $i % 2 == 0 && $s [ $i ] == '$' )
$sum *= 21;
else if ( $s [ $i ] == '$' )
$sum *= 5;
}
return $sum ;
}
$str = "s\$\$e\$" ;
echo countStrings( $str );
?>
|
Javascript
<script>
function countStrings( s)
{
let sum = 1;
for (let i = 0; i < s.length; i++)
{
if (i % 2 == 0 && s[i] == '$' )
sum *= 21;
else if (s[i] == '$' )
sum *= 5;
}
return sum;
}
let str = "s$$e$" ;
document.write(countStrings(str));
</script>
|
Time complexity: O(n) where n is length of given string
Auxiliary space: O(1)