 Open in App
Not now

# Print all Substrings of a String that has equal number of vowels and consonants

• Last Updated : 14 Mar, 2023

Given a string S, the task is to print all the substrings of a string that has an equal number of vowels and consonants.

Examples:

Input: “geeks”
Output: “ge”, “geek”, “eeks”, “ek”

Input: “coding”
Output: “co”, “codi”, “od”, “odin”, “di”, “in”

Naive Approach: The basic approach to solve this problem is to generate all the substrings and then for each substring count the number of vowels and consonants present in it. If they are equal print it.

Time complexity: O(N^3)
Auxiliary Space: O(1)

Efficient Approach: To solve the problem follow the below idea:

In this approach, we traverse through two loops and store the start and end indices of every substring in a vector that has an equal number of vowels and consonants.

Steps involved in this approach:

• First, we traverse a for loop which depicts the starting positions of the substrings.
• Then an inner loop is traversed which in every iteration checks if the current character is a vowel or consonant.
• We increment the count of vowels or consonants based on these if conditions.
• If at any instance the number of vowels and consonants are equal then we add the start and end indices of that current substring.
• After both loops are traversed then print all the substrings with the indices present in the vector.

Below is the code for the above approach:

## C++

 `// C++ code for above approach` `#include ` `using` `namespace` `std;`   `// Initialising vector` `vector > ans;`   `// Function to check if character is` `// vowel or consonent.` `bool` `isVowel(``char` `c)` `{` `    ``return` `c == ``'a'` `|| c == ``'e'` `|| c == ``'i'` `|| c == ``'o'` `           ``|| c == ``'u'``;` `}`   `// Function to print all` `// possible substring` `void` `all_substring(string s, ``int` `n)` `{` `    ``for` `(``int` `i = 0; i < n; i++) {` `        ``int` `count_vowel = 0, count_consonant = 0;` `        ``for` `(``int` `j = i; j < n; j++) {`   `            ``// If vowel increase its count` `            ``if` `(isVowel(s[j]))` `                ``count_vowel++;`   `            ``// If consonent increase` `            ``// its count` `            ``else` `                ``count_consonant++;`   `            ``// If equal vowel and consonant` `            ``// in the substring store the` `            ``// index of the starting and` `            ``// ending point of that substring` `            ``if` `(count_vowel == count_consonant)` `                ``ans.push_back({ i, j });` `        ``}` `    ``}`   `    ``// Printing all substrings` `    ``for` `(``auto` `x : ans) {` `        ``int` `l = x.first;` `        ``int` `r = x.second;`   `        ``cout << s.substr(l, r - l + 1) << endl;` `    ``}` `}`   `// Driver Code` `int` `main()` `{` `    ``string s = ``"geeks"``;` `    ``int` `n = s.size();`   `    ``// Function call` `    ``all_substring(s, n);` `    ``return` `0;` `}`

## Java

 `// Java code for above approach` `import` `java.util.ArrayList;` `import` `java.util.List;`   `public` `class` `Main {` `    ``// List to store the starting and ending indices of the substrings` `    ``static` `List ans = ``new` `ArrayList<>();`   `    ``// Function to check if a character is a vowel` `    ``static` `boolean` `isVowel(``char` `c) {` `        ``return` `c == ``'a'` `|| c == ``'e'` `|| c == ``'i'` `|| c == ``'o'` `|| c == ``'u'``;` `    ``}`   `    ``// Custom implementation of the Pair class` `    ``static` `class` `Pair {` `        ``int` `key, value;` `        ``Pair(``int` `key, ``int` `value) {` `            ``this``.key = key;` `            ``this``.value = value;` `        ``}` `    ``}`   `    ``// Function to find all substrings with equal number of vowels and consonants` `    ``static` `void` `allSubstring(String s, ``int` `n) {` `        ``// Iterating over all substrings` `        ``for` `(``int` `i = ``0``; i < n; i++) {` `            ``int` `countVowel = ``0``, countConsonant = ``0``;` `            ``for` `(``int` `j = i; j < n; j++) {` `                ``// If character is a vowel, increase the count of vowels` `                ``if` `(isVowel(s.charAt(j))) {` `                    ``countVowel++;` `                ``} ` `                ``// If character is a consonant, increase the count of consonants` `                ``else` `{` `                    ``countConsonant++;` `                ``}` `                ``// If the count of vowels and consonants is equal in the current substring` `                ``if` `(countVowel == countConsonant) {` `                    ``// Add the indices of the starting and ending of the substring to the list` `                    ``ans.add(``new` `Pair(i, j));` `                ``}` `            ``}` `        ``}` `        ``// Print all the substrings with equal number of vowels and consonants` `        ``for` `(Pair x : ans) {` `            ``int` `l = x.key;` `            ``int` `r = x.value;` `            ``System.out.println(s.substring(l, r + ``1``));` `        ``}` `    ``}`   `    ``public` `static` `void` `main(String[] args) {` `        ``// Input string` `        ``String s = ``"geeks"``;` `        ``int` `n = s.length();` `        ``// Call to the function to find all substrings` `        ``allSubstring(s, n);` `    ``}` `}` `   `  `// This code is contributed by Utkarsh Kumar.`

## Python3

 `# Python code for above approach`   `# Initialising list` `ans ``=` `[]`   `# Function to check if character is` `# vowel or consonent.` `def` `isVowel(c):` `    ``return` `c ``in` `[``'a'``, ``'e'``, ``'i'``, ``'o'``, ``'u'``]`   `# Function to print all` `# possible substring` `def` `all_substring(s, n):` `    ``for` `i ``in` `range``(n):` `        ``count_vowel ``=` `0` `        ``count_consonant ``=` `0` `        ``for` `j ``in` `range``(i, n):`   `            ``# If vowel increase its count` `            ``if` `isVowel(s[j]):` `                ``count_vowel ``+``=` `1`   `            ``# If consonent increase` `            ``# its count` `            ``else``:` `                ``count_consonant ``+``=` `1`   `            ``# If equal vowel and consonant` `            ``# in the substring store the` `            ``# index of the starting and` `            ``# ending point of that substring` `            ``if` `count_vowel ``=``=` `count_consonant:` `                ``ans.append((i, j))`   `    ``# Printing all substrings` `    ``for` `x ``in` `ans:` `        ``l ``=` `x[``0``]` `        ``r ``=` `x[``1``]`   `        ``print``(s[l:r ``+` `1``])`   `# Driver Code` `s ``=` `"geeks"` `n ``=` `len``(s)`   `# Function call` `all_substring(s, n)`   `# This code is contributed by prasad264`

## C#

 `// C# code for above approach` `using` `System;` `using` `System.Collections.Generic;`   `public` `class` `GfG ` `{ `   `  ``// Initialising vector` `  ``static` `List > ans=``new` `List>();`   `  ``// Function to check if character is` `  ``// vowel or consonent.` `  ``static` `bool` `isVowel(``char` `c)` `  ``{` `    ``return` `c == ``'a'` `|| c == ``'e'` `|| c == ``'i'` `|| c == ``'o'` `      ``|| c == ``'u'``;` `  ``}`   `  ``// Function to print all` `  ``// possible substring` `  ``static` `void` `all_substring(``string` `s, ``int` `n)` `  ``{` `    ``for` `(``int` `i = 0; i < n; i++) {` `      ``int` `count_vowel = 0, count_consonant = 0;` `      ``for` `(``int` `j = i; j < n; j++) {`   `        ``// If vowel increase its count` `        ``if` `(isVowel(s[j]))` `          ``count_vowel++;`   `        ``// If consonent increase` `        ``// its count` `        ``else` `          ``count_consonant++;`   `        ``// If equal vowel and consonant` `        ``// in the substring store the` `        ``// index of the starting and` `        ``// ending point of that substring` `        ``if` `(count_vowel == count_consonant)` `          ``ans.Add(Tuple.Create(i, j));` `      ``}` `    ``}`   `    ``// Printing all substrings` `    ``foreach` `(``var` `x ``in` `ans)` `    ``{` `      ``int` `l = x.Item1;` `      ``int` `r = x.Item2;`   `      ``Console.WriteLine(s.Substring(l, r - l + 1));` `    ``}` `  ``}`   `  ``// Driver Code` `  ``public` `static` `void` `Main(String[] args) ` `  ``{` `    ``string` `s = ``"geeks"``;` `    ``int` `n = s.Length;`   `    ``// Function call` `    ``all_substring(s, n);` `  ``}` `}`

## Javascript

 `// Javascript code for above approach`   `// Initialising vector` `let ans = [];`   `// Function to check if character is` `// vowel or consonent.` `function` `isVowel(c)` `{` `    ``return` `c == ``'a'` `|| c == ``'e'` `|| c == ``'i'` `|| c == ``'o'` `           ``|| c == ``'u'``;` `}`   `// Function to print all` `// possible substring` `function` `all_substring(s, n)` `{` `    ``for` `(let i = 0; i < n; i++) {` `        ``let count_vowel = 0, count_consonant = 0;` `        ``for` `(let j = i; j < n; j++) {`   `            ``// If vowel increase its count` `            ``if` `(isVowel(s[j]))` `                ``count_vowel++;`   `            ``// If consonent increase` `            ``// its count` `            ``else` `                ``count_consonant++;`   `            ``// If equal vowel and consonant` `            ``// in the substring store the` `            ``// index of the starting and` `            ``// ending point of that substring` `            ``if` `(count_vowel == count_consonant)` `                ``ans.push({ first:i, second:j });` `        ``}` `    ``}`   `    ``// Printing all substrings` `    ``for` `(let i = 0; i < ans.length; i++) {` `        ``let l = ans[i].first;` `        ``let r = ans[i].second;` `        ``document.write(s.substr(l, r - l + 1));` `        ``document.write(``"
"``);` `    ``}` `}`   `// Driver Code`   `    ``let s = ``"geeks"``;` `    ``let n = s.length;`   `    ``// Function call` `    ``all_substring(s, n);`

Output

```ge
geek
eeks
ek```

Time Complexity: O(N2)
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up
Related Articles