Senior
Algorithmic task: count the number of strings in an array where vowels are more than consonants.
sobes.tech AI
Answer from AI
To solve the task of counting the number of strings where vowels are more than consonants, you can iterate through the array of strings and for each string, count the vowels and consonants.
Example in Node.js:
function countStringsWithMoreVowels(arr) {
const vowels = new Set(['а','е','ё','и','о','у','ы','э','ю','я',
'А','Е','Ё','И','О','У','Ы','Э','Ю','Я']);
let count = 0;
for (const str of arr) {
let vowelsCount = 0;
let consonantsCount = 0;
for (const char of str) {
if (/[а-яА-Я]/.test(char)) { // Check if the character is a Russian letter
if (vowels.has(char)) {
vowelsCount++;
} else {
consonantsCount++;
}
}
}
if (vowelsCount > consonantsCount) {
count++;
}
}
return count;
}
// Usage example:
const arr = ["привет", "мир", "океан", "яблоко"];
console.log(countStringsWithMoreVowels(arr)); // Outputs the number of strings with more vowels
In this code:
- A set of vowels is used for quick lookup.
- For each string, vowels and consonants are counted, considering only Russian letters.
- If vowels are more, the counter is incremented.
- The total count of such strings is returned.