Sort an Array of Strings according to the number of Vowels in them

Given an array arr[] of N strings, the task is to sort these strings according to the numbers of vowels in them.

Examples: 

Input: arr[] = { “geeks”, “for”, “coding” } 
Output: for, coding, geeks 
for -> o = 1 vowel 
coding -> o, i = 2 vowels 
geeks -> e, e = 2 vowels

Input: arr[] = { “lmno”, “pqrst”, “aeiou”, “xyz” } 
Output: pqrst, xyz, lmno, aeiou  

Approach: The idea is to store each element with its number of vowels in a vector pair and then sort all the elements of the vector according to the number of vowels stored. Finally, print the strings in order.



Below is the implementation of the above approach:  

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to check the Vowel
bool isVowel(char ch)
{
    ch = toupper(ch);
    return (ch == 'A' || ch == 'E'
            || ch == 'I' || ch == 'O'
            || ch == 'U');
}
 
// Returns count of vowels in str
int countVowels(string str)
{
    int count = 0;
    for (int i = 0; i < str.length(); i++)
        if (isVowel(str[i])) // Check for vowel
            ++count;
    return count;
}
 
// Function to sort the array according to
// the number of the vowels
void sortArr(string arr[], int n)
{
    // Vector to store the number of vowels
    // with respective elements
    vector<pair<int, string> > vp;
 
    // Inserting number of vowels
    // with respective strings
    // in the vector pair
    for (int i = 0; i < n; i++) {
 
        vp.push_back(
            make_pair(
                countVowels(
                    arr[i]),
                arr[i]));
    }
 
    // Sort the vector, this will sort the pair
    // according to the number of vowels
    sort(vp.begin(), vp.end());
 
    // Print the sorted vector content
    for (int i = 0; i < vp.size(); i++)
        cout << vp[i].second << " ";
}
 
// Driver code
int main()
{
    string arr[] = { "lmno", "pqrst",
                     "aeiou", "xyz" };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    sortArr(arr, n);
 
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
import java.util.*;
import java.lang.*;
import java.io.*;
 
class GFG{
 
static class pair
{
    int first;
    String second;
     
    pair(int first,String second)
    {
        this.first = first;
        this.second = second;
    }
}
 
// Function to check the Vowel
static boolean isVowel(char ch)
{
    ch = Character.toUpperCase(ch);
    return (ch == 'A' || ch == 'E' ||
            ch == 'I' || ch == 'O' ||
            ch == 'U');
}
 
// Returns count of vowels in str
static int countVowels(String str)
{
    int count = 0;
    for(int i = 0; i < str.length(); i++)
     
        // Check for vowel
        if (isVowel(str.charAt(i)))
            ++count;
             
    return count;
}
 
// Function to sort the array according to
// the number of the vowels
static void sortArr(String arr[], int n)
{
     
    // Vector to store the number of vowels
    // with respective elements
    ArrayList<pair> vp = new ArrayList<>();
 
    // Inserting number of vowels
    // with respective strings
    // in the vector pair
    for(int i = 0; i < n; i++)
    {
        vp.add(new pair(countVowels(arr[i]),
                                    arr[i]));
    }
 
    // Sort the vector, this will sort the pair
    // according to the number of vowels
    Collections.sort(vp, (a, b) -> a.first - b.first);
     
    // Print the sorted vector content
    for(int i = 0; i < vp.size(); i++)
        System.out.print(vp.get(i).second + " ");
}
 
// Driver code
public static void main(String[] args)
{
    String arr[] = { "lmno", "pqrst",
                     "aeiou", "xyz" };
    int n = arr.length;
     
    sortArr(arr, n);
}
}
 
// This code is contributed by offbeat

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach
 
# Function to check the Vowel
def isVowel(ch) :
     
    ch = ch.upper();
    return (ch == 'A' or ch == 'E'or ch == 'I' or
            ch == 'O'or ch == 'U');
 
# Returns count of vowels in str
def countVowels(string) :
 
    count = 0;
    for i in range(len(string)) :
         
        # Check for vowel
        if (isVowel(string[i])) :
            count += 1;
             
    return count;
 
# Function to sort the array according to
# the number of the vowels
def sortArr(arr, n) :
     
    # Vector to store the number of vowels
    # with respective elements
    vp = [];
 
    # Inserting number of vowels
    # with respective strings
    # in the vector pair
    for i in range(n) :
 
        vp.append((countVowels(arr[i]),arr[i]));
 
    # Sort the vector, this will sort the pair
    # according to the number of vowels
    vp.sort()
 
    # Print the sorted vector content
    for i in range(len(vp)) :
        print(vp[i][1], end= " ");
 
# Driver code
if __name__ == "__main__" :
    arr = [ "lmno", "pqrst","aeiou", "xyz" ];
    n = len(arr);
 
    sortArr(arr, n);
 
# This code is contributed by AnkitRai01

chevron_right


Output: 

pqrst xyz lmno aeiou




 

Time Complexity: O(N*log N)
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : AnkitRai01, offbeat