Vowel Recognition

{a,e,i,o,u,A,E,I,O,U}

Natural Language Understanding is the subdomain of Natural Language Processing where people used to design AI-based applications have the ability to understand 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 a 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.

Example:

Input:  s = "baceb"
Output: 16

Approach

Java

public class VowelRecognition {
    public static void main(String[] args) {

        String str = "baceb";

        vowelRecognition(str);

    }

    static boolean isvowel(char c) {
        if (c == 'a' || c == 'e' || c == 'i' || c == 'o'
 || c == 'u')
            return true;
        return false;
    }

    static void vowelRecognition(String str) {
        str = str.toLowerCase();
        long arr[] = new long[str.length()];
        for (int i = 0; i < str.length(); i++) {
            if (isvowel(str.charAt(i)))
                arr[i] = 1;
            else
                arr[i] = 0;
        }

        long cnt = 0;

        for (int i = 1; i < str.length(); i++)
            arr[i] = arr[i] + arr[i - 1];

        long suffix[] = new long[str.length()];
        for (int i = 0; i < str.length(); i++)
            suffix[i] = 0;
        suffix[str.length() - 1] = arr[str.length() - 1];
        for (int i = str.length() - 2; i >= 0; i--)
            suffix[i] = arr[i] + suffix[i + 1];

        cnt += suffix[0];
        for (int i = 1; i < str.length(); i++) {
            if (suffix[i] == suffix[i - 1])
                cnt += suffix[i];
            else {
                long diff = suffix[i - 1] - suffix[i];
                cnt += suffix[i] - diff * (str.length() - i);
            }
        }
        System.out.println(cnt);
    }

}

C++

#include <bits/stdc++.h>
using namespace std;
bool isvowel(char c)
{
    if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
        return true;
    return false;
}

void vowelRecognition(string str)
{
    for (long long int i = 0; i < str.size(); i++)
    {
        if (str[i] >= 'A' && str[i] <= 'Z')
            str[i] = str[i] + 32;
    }
    long long int arr[str.size()];
    for (long long int i = 0; i < str.size(); i++)
    {
        if (isvowel(str[i]))
            arr[i] = 1;
        else
            arr[i] = 0;
    }

    long long int cnt = 0;

    for (long long int i = 1; i < str.size(); i++)
        arr[i] = arr[i] + arr[i - 1];

    long long int suffix[str.size()];
    for (long long int i = 0; i < str.size(); i++)
        suffix[i] = 0;
    suffix[str.size() - 1] = arr[str.size() - 1];
    for (long long int i = str.size() - 2; i >= 0; i--)
        suffix[i] = arr[i] + suffix[i + 1];

    cnt += suffix[0];
    for (long long int i = 1; i < str.size(); i++)
    {
        if (suffix[i] == suffix[i - 1])
            cnt += suffix[i];
        else
        {
            long long int diff = suffix[i - 1] - suffix[i];
            cnt += suffix[i] - diff * (str.size() - i);
        }
    }
    cout << cnt << "\n";
}
int main()
{

    string str = "baceb";

    vowelRecognition(str);

    return 0;
}


Read Interview Questions

Exception Handling Interview Questions

DBMS Interview Questions Set -1

DBMS Interview Questions Set -2

SQL Interview Question Set -1

SQL Interview Question Set -2

JPA Interview Questions Set -1

JPA Interview Question Set -2

Hibernate Interview Questions

Spring Boot Interview Questions Set 1

Spring Boot Interview Questions Set 2


2 comments: