Problem link - Find Row with Maximum Vowels in
Problem Statement:
You are given a grid of lowercase English characters with R rows and C columns. Your task is to find the row with the maximum number of vowels (a, e, i, o, u). If two rows have the same maximum number of vowels, return the smaller row number.
Approach:
The key idea is to count the number of vowels in each row of the grid and find the row with the highest count. We first read the grid of characters with n rows and m columns. For each row, we check each character to see if it is a vowel (a, e, i, o, u) and count how many vowels are present. While doing this, we keep track of the row with the maximum vowel count. If a row has more vowels than the previous maximum, we update the maximum and store the current row’s index. If multiple rows have the same maximum count, we retain the smaller row index. Finally, we print the index of the row with the most vowels, adjusting for 1-based indexing.
Time Complexity:
- O(n * m), where n is the number of rows and m is the number of columns, as we iterate over every element in the grid once.
Space Complexity:
- O(n * m) for storing the grid in memory.