Amazing Subarrays(cpp,interviewbit)
Amazing subarray(cpp,interviewbit)
You are given a string S, and you have to find all the amazing substrings of S.
Amazing Substring is one that starts with a vowel (a, e, i, o, u, A, E, I, O, U).
=================================================
Example
Input
ABEC
Output
6
Explanation
Amazing substrings of given string are :
1. A
2. AB
3. ABE
4. ABEC
5. E
6. EC
here number of substrings are 6 and 6 % 10003 = 6.
===========================================================================================
int Solution::solve(string s)
{
long int count=0;
long int x=0;
unordered_map<char,int>m={{'a',1},{'e',1},{'i',1},{'o',1},{'u',1},{'A',1},{'E',1},{'O',1},{'I',1},{'U',1}};
for(long int i=s.size()-1;i>=0;i--)
{
x++;
if(m.find(s[i]) != m.end())
{
count=count+x;
}
}
return count%10003;
}
Comments
Post a Comment