carloscn / structstudy

Leetcode daily trainning by using C/C++/RUST programming.
4 stars 1 forks source link

leetcode2586: Count the Number of Vowel Strings in Range #424

Open carloscn opened 10 months ago

carloscn commented 10 months ago

Description

You are given a 0-indexed array of string words and two integers left and right.

A string is called a vowel string if it starts with a vowel character and ends with a vowel character where vowel characters are 'a', 'e', 'i', 'o', and 'u'.

Return the number of vowel strings words[i] where i belongs to the inclusive range [left, right].

Example 1:

Input: words = ["are","amy","u"], left = 0, right = 2 Output: 2 Explanation:

Example 2:

Input: words = ["hey","aeo","mu","ooo","artro"], left = 1, right = 4 Output: 3 Explanation:

Constraints:

1 <= words.length <= 1000 1 <= words[i].length <= 10 words[i] consists of only lowercase English letters. 0 <= left <= right < words.length

carloscn commented 10 months ago

Analysis

static bool is_vowel(char c)
{
    static const char* vowels = "aeiouAEIOU";
    return strchr(vowels, c) != NULL;
}

int32_t vowel_strings(const char* words[], size_t words_size, size_t left, size_t right)
{
    int32_t ret = 0;

    UTILS_CHECK_PTR(words);
    UTILS_CHECK_LEN(words_size);

    if ((right >= words_size) || (left > right)) {
        ret = -1;
        goto finish;
    }

    for (size_t i = left; i <= right; i ++) {
        const char *e = words[i];
        if (NULL == e) {
            continue;
        }

        size_t len = strlen(e);
        if ((len > 0) && is_vowel(e[0]) && is_vowel(e[len - 1])) {
            ret ++;
        }
    }

finish:
    return ret;
}
carloscn commented 10 months ago

Code

https://review.gerrithub.io/c/carloscn/structstudy/+/1172259 https://github.com/carloscn/structstudy/commit/eabf39058dc0c7a6b03adbd7fc9da9f66d423af2