Wednesday 2 September 2020

Vowel Recognition Sum of vowels in all substrings of a string.#hackerearth | using prefix_sum | Python.. and in C++

 Natural Language Understanding is the subdomain of Natural Language Processing where people used to design AI based applications have ability to understand the human languages. HashInclude Speech Processing team has a project named Virtual Assistant. For this project they appointed you as a data engineer (who has good knowledge of creating clean datasets by writing efficient code). As a data engineer your first task is to make vowel recognition dataset. In this task you have to find the presence of vowels in all possible substrings of the given string. For each given string you have to print the total number of vowels.

Input First line contains an integer T, denoting the number of test cases. Each of the next lines contains a string, string contains both lower case and upper case . Output Print the vowel sum Answer for each test case should be printed in a new line. SAMPLE INPUT 1 baceb SAMPLE OUTPUT 16 Explanation First line is number of input string, In given example, string is "baceb" so the substrings will be like -"b, ba, bac, bace, a, ac, ace, aceb, c, ce, ceb, e, eb, baceb" now the number of vowels in each substring will be 0, 1, 1, 2, 1, 1, 2, 2, 0, 1, 1, 1, 1, 2 and the total number will be sum of all presence which is 16.


Here is the solution in python using prefix sum approach..this is my you tube chennal..



in C++ :

#include<bits/stdc++.h>
using namespace std;
int main() {
int t, arr[10] = {'A','E','I','O','U','a','e','i','o','u'},len;
long int count;
string s;
cin >> t;
while(t--) {
count = 0;
cin >> s;
len = s.length();
long int ps_arr[len]; int i=1;
ps_arr[0] = len;
while(i<len) {
ps_arr[i] = (len-i)+ps_arr[i-1]-i;
i++;
}
i = 0;
while(i<len) {
if(find(arr,arr+10,s.at(i)) != arr+10) {
count += ps_arr[i];
}
i++;
}
cout << count << "\n";
}
}

1 comment:

  1. How to register a new bet365 account | Sportsbook - Airjordan 7
    Bet365 is one of the best new air jordan 18 retro yellow betting sites in the world when it comes to air jordan 18 retro men blue discount mobile betting. We'll show you how air jordan 18 retro men discount you can use the 토토사이트 Bet365 best air jordan 18 retro men app

    ReplyDelete