#1704. Determine if String Halves Are Alike

ยท

1 min read

https://leetcode.com/problems/determine-if-string-halves-are-alike/?envType=daily-question&envId=2024-01-12

var halvesAreAlike = function (s) {
    const mid = (s.length / 2)

    const firstHalf = s.substring(0, mid)
    const secondHalf = s.substring(mid)

    function checkForVowels(string) {
        const vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']
        let count = 0

        for (let i of string) {
            if (vowels.includes(i)) count++
        }

        return count
    }

    return checkForVowels(firstHalf) === checkForVowels(secondHalf)
};

This question is a very easy one.

Split the array into two.

The array are even according to the question so no need to border about odd numbers.

Loop through each halves and count the number of vowels present, whether uppercase or lowercase.

Return the count and compare the count of both halves.

ย