Javatpoint Logo
Javatpoint Logo

Print all Substrings of a String that has equal number of vowels and consonants in C/C++

In this lesson, we'll look at how to display all substrings of a string that have the same number of vowels and consonants in C and C++.

Provided a string S, the goal is to show all the substrings of that string that have an equal amount of vowels and consonants.

Examples:


The basic technique to overcoming this challenge is to produce all the substrings and then count the number of vowels and consonants in each substring. If they are the same, print it.

Time complexity will be O(N^3).

Auxiliary Space will be O(1).

Reliable Strategy: To find the solution, use the following concept:

In this method, we use two loops to keep the start and end indices of each substring in a vector with an equal amount of vowels and consonants.

This method includes the following steps:

  • First, we go through a for loop that shows the starting places of the substrings.
  • Then an inner loop is explored, which verifies if the present character is a vowel or a consonant on each iteration.
  • Based on these if conditions, we increase the number of vowels or consonants.
  • If the number of vowels and consonants is equal at any point, we add the start and end indices of the current substring.
  • After traversing both loops, display all the substrings with the indices present in the vector.

The following is the code for the aforementioned method:

C++ Program:

Output

po
poli
police
ol
olic
li
lice
ic
ce

Time Complexity will be O(N2).

Auxiliary Space will be O(N).


Next TopicAccumulator in C++





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA