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++
#include <bits/stdc++.h>
using namespace std;
bool isVowel( char ch)
{
ch = toupper (ch);
return (ch == 'A' || ch == 'E'
|| ch == 'I' || ch == 'O'
|| ch == 'U' );
}
int countVowels(string str)
{
int count = 0;
for ( int i = 0; i < str.length(); i++)
if (isVowel(str[i]))
++count;
return count;
}
void sortArr(string arr[], int n)
{
vector<pair< int , string> > vp;
for ( int i = 0; i < n; i++) {
vp.push_back(
make_pair(
countVowels(
arr[i]),
arr[i]));
}
sort(vp.begin(), vp.end());
for ( int i = 0; i < vp.size(); i++)
cout << vp[i].second << " " ;
}
int main()
{
string arr[] = { "lmno" , "pqrst" ,
"aeiou" , "xyz" };
int n = sizeof (arr) / sizeof (arr[0]);
sortArr(arr, n);
return 0;
}
|
Java
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;
}
}
static boolean isVowel( char ch)
{
ch = Character.toUpperCase(ch);
return (ch == 'A' || ch == 'E' ||
ch == 'I' || ch == 'O' ||
ch == 'U' );
}
static int countVowels(String str)
{
int count = 0 ;
for ( int i = 0 ; i < str.length(); i++)
if (isVowel(str.charAt(i)))
++count;
return count;
}
static void sortArr(String arr[], int n)
{
ArrayList<pair> vp = new ArrayList<>();
for ( int i = 0 ; i < n; i++)
{
vp.add( new pair(countVowels(arr[i]),
arr[i]));
}
Collections.sort(vp, (a, b) -> a.first - b.first);
for ( int i = 0 ; i < vp.size(); i++)
System.out.print(vp.get(i).second + " " );
}
public static void main(String[] args)
{
String arr[] = { "lmno" , "pqrst" ,
"aeiou" , "xyz" };
int n = arr.length;
sortArr(arr, n);
}
}
|
Python3
def isVowel(ch) :
ch = ch.upper();
return (ch = = 'A' or ch = = 'E' or ch = = 'I' or
ch = = 'O' or ch = = 'U' );
def countVowels(string) :
count = 0 ;
for i in range ( len (string)) :
if (isVowel(string[i])) :
count + = 1 ;
return count;
def sortArr(arr, n) :
vp = [];
for i in range (n) :
vp.append((countVowels(arr[i]),arr[i]));
vp.sort()
for i in range ( len (vp)) :
print (vp[i][ 1 ], end = " " );
if __name__ = = "__main__" :
arr = [ "lmno" , "pqrst" , "aeiou" , "xyz" ];
n = len (arr);
sortArr(arr, n);
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static bool isVowel( char ch)
{
ch = char .ToUpper(ch);
return (ch == 'A' || ch == 'E'
|| ch == 'I' || ch == 'O'
|| ch == 'U' );
}
static int countVowels( string str)
{
int count = 0;
for ( int i = 0; i < str.Length; i++)
if (isVowel(str[i]))
++count;
return count;
}
static void sortArr( string [] arr, int n)
{
List<Tuple< int , string >> vp = new List<Tuple< int , string >>();
for ( int i = 0; i < n; i++)
{
vp.Add( new Tuple< int , string >(countVowels(arr[i]), arr[i]));
}
vp.Sort();
for ( int i = 0; i < vp.Count; i++)
Console.Write(vp[i].Item2 + " " );
}
static void Main()
{
string [] arr = { "lmno" , "pqrst" ,
"aeiou" , "xyz" };
int n = arr.Length;
sortArr(arr, n);
}
}
|
Javascript
<script>
function isVowel(ch)
{
ch = ch.toUpperCase();
return (ch == 'A' || ch == 'E'
|| ch == 'I' || ch == 'O'
|| ch == 'U' );
}
function countVowels(str)
{
let count = 0;
for (let i = 0; i < str.length; i++)
if (isVowel(str[i]))
++count;
return count;
}
function sortArr(arr, n)
{
let vp = [];
for (let i = 0; i < n; i++)
{
vp.push([countVowels(arr[i]), arr[i]]);
}
vp.sort();
for (let i = 0; i < vp.length; i++)
document.write(vp[i][1] + " " );
}
let arr = [ "lmno" , "pqrst" , "aeiou" , "xyz" ];
let n = arr.length;
sortArr(arr, n);
</script>
|
Output:
pqrst xyz lmno aeiou
Time Complexity: O(N*log N), for sorting the array.
Auxiliary Space: O(N), for storing the string in an extra array.
The approach sorts the input list of strings based on the number of vowels they contain using sorting function
C++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int countVowels(string word)
{
string vowels = "aeiouAEIOU" ;
int count = 0;
for ( int i = 0; i < word.length(); i++) {
if (vowels.find(word[i]) != string::npos) {
count++;
}
}
return count;
}
vector<string> sortStrings(vector<string> strings)
{
sort(strings.begin(), strings.end(), []( const string& s1, const string& s2) {
return countVowels(s1) < countVowels(s2);
});
return strings;
}
int main()
{
vector<string> strings = { "lmno" , "pqrst" , "aeiou" , "xyz" };
vector<string> sortedStrings = sortStrings(strings);
for ( const auto & s : sortedStrings) {
cout << s << " " ;
}
cout << endl;
return 0;
}
|
Java
import java.util.*;
public class GFG {
public static int countVowels(String word)
{
String vowels = "aeiouAEIOU" ;
int count = 0 ;
for ( int i = 0 ; i < word.length(); i++) {
if (vowels.indexOf(word.charAt(i)) != - 1 ) {
count++;
}
}
return count;
}
public static List<String>
sortStrings(List<String> strings)
{
Collections.sort(strings, new Comparator<String>() {
@Override
public int compare(String s1, String s2)
{
return countVowels(s1) - countVowels(s2);
}
});
return strings;
}
public static void main(String[] args)
{
List<String> strings = Arrays.asList(
"lmno" , "pqrst" , "aeiou" , "xyz" );
List<String> sortedStrings = sortStrings(strings);
System.out.println(sortedStrings);
}
}
|
Python3
def count_vowels(word):
vowels = "aeiouAEIOU"
return sum (c in vowels for c in word)
def sort_strings(strings):
return sorted (strings, key = count_vowels)
if __name__ = = "__main__" :
strings = [ "lmno" , "pqrst" , "aeiou" , "xyz" ]
sorted_strings = sort_strings(strings)
print (sorted_strings)
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
public class GFG
{
public static int CountVowels( string word)
{
string vowels = "aeiouAEIOU" ;
int count = 0;
for ( int i = 0; i < word.Length; i++)
{
if (vowels.IndexOf(word[i]) != -1)
{
count++;
}
}
return count;
}
public static List< string >
SortStrings(List< string > strings)
{
strings.Sort((s1, s2) => CountVowels(s1) - CountVowels(s2));
return strings;
}
public static void Main( string [] args)
{
List< string > strings = new List< string >
{
"lmno" , "pqrst" , "aeiou" , "xyz"
};
List< string > sortedStrings = SortStrings(strings);
Console.WriteLine( string .Join( " " , sortedStrings));
}
}
|
Javascript
function count_vowels(word) {
const vowels = "aeiouAEIOU" ;
return Array.from(word).filter(c => vowels.includes(c)).length;
}
function sort_strings(strings) {
return strings.sort((a, b) => count_vowels(a) - count_vowels(b));
}
const strings = [ "lmno" , "pqrst" , "aeiou" , "xyz" ];
const sorted_strings = sort_strings(strings);
console.log(sorted_strings);
|
Output
['pqrst', 'xyz', 'lmno', 'aeiou']
Time Complexity: O(N log N)
Auxiliary Space: O(N)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
09 Mar, 2023
Like Article
Save Article