Header Ad

HackerEarth Can you count? problem solution

In this HackerEarth Can you count? problem solution You are given a string s consisting of lowercase English letters and/or '_' (underscore).
You have to replace all underscores (if any) with vowels present in the string.

The rule you follow is:
Each underscore can be replaced with any one of the vowel(s) that came before it.

You have to tell the total number of distinct strings we can generate following the above rule.


HackerEarth Can you count? problem solution


HackerEarth Can you count? problem solution.

import java.util.*;

public class Sol {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);

int t = in.nextInt();
while(t-- > 0) {
String s = in.next();

long count = 1;

HashSet<Character> vowel = new HashSet<>();
HashSet<Character> is_vowel = new HashSet<>();
is_vowel.add('a'); is_vowel.add('e'); is_vowel.add('i'); is_vowel.add('o'); is_vowel.add('u');

for(int i=0;i<s.length();i++) {
if(is_vowel.contains(s.charAt(i)))
vowel.add(s.charAt(i));

if(s.charAt(i) == '_') {
long vowels_till_now = vowel.size();
count *= vowels_till_now;
}
}

System.out.println(count);
}
}
}

Second solution

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
int t;
cin>>t;
assert(t>=1 && t<=4e3);
int sum=0;
while(t--)
{
string s;
map<char,bool>mp;
char a[]={'a','e','i','o','u'};
cin>>s;ll ans=1;
assert(s.size()>=1 && s.size()<=1e5);
sum+=s.size();
for(int i=0;i<s.size();i++)
{
assert((s[i]>='a' && s[i]<='z') || s[i]=='_');
mp[s[i]]=1;
if(s[i]=='_')
{
int cnt=0;
for(int j=0;j<5;j++)
if(mp[a[j]])
cnt++;
ans=ans*(ll)cnt;
}
}
cout<<ans<<"\n";
}
return 0;
}

Post a Comment

0 Comments