Print All Substrings Of A String That Has Equal Number Of Vowels And Consonants In Cc++ Tpoint Tec - C++ Programming Tutorial
C++ Course / Data Structures / Print All Substrings Of A String That Has Equal Number Of Vowels And Consonants In Cc++ Tpoint Tec

Print All Substrings Of A String That Has Equal Number Of Vowels And Consonants In Cc++ Tpoint Tec

BLUF: Mastering Print All Substrings Of A String That Has Equal Number Of Vowels And Consonants In Cc++ Tpoint Tec is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Print All Substrings Of A String That Has Equal Number Of Vowels And Consonants In Cc++ Tpoint Tec

C++ is renowned for its efficiency. Learn how Print All Substrings Of A String That Has Equal Number Of Vowels And Consonants In Cc++ Tpoint Tec enables low-level control and high-performance computing in the tutorial below.

In this tutorial, we will explore the process of showcasing all substrings within a string that possess an equal count of vowels and consonants in C and C++.

Given a string S, the objective is to display all the substrings within the string that contain an equal number of vowels and consonants.

Examples:

Example

Enter "police" as an input.
  "po", "poli", "police", "ol", "olic", "li", "lice", "ic", "ce" as output
Example

"coding" as input
   "co", "codi", "od", "odin", "di", "in" as output

The fundamental method to tackle this issue involves generating all substrings and subsequently calculating the quantity of vowels and consonants in each substring. If the counts are equal, display the substring.

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 technique, a pair of loops is employed to store the beginning and concluding indexes of every subsequence within an array containing an equivalent number 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 subsequent code snippet showcases the previously mentioned function:

C++ Program:

Example

#include <bits/stdc++.h>
using namespace std;

vector<pair<int, int> > ans;

bool isVowel(char c)
{
    return c == 'a' || c == 'e' || c == 'i' || c == 'o'
           || c == 'u';
}

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 (isVowel(s[j]))
                count_vowel++;

            else
                count_consonant++;

            if (count_vowel == count_consonant)
                ans.push_back({ i, j });
        }
    }

    for (auto x : ans) {
        int l = x.first;
        int r = x.second;

        cout << s.substr(l, r - l + 1) << endl;
    }
}
int main()
{
    string s = "police";
    int n = s.size();
    all_substring(s, n);
    return 0;
}

Output

Output

po
poli
police
ol
olic
li
lice
ic
ce

Time Complexity will be O(N 2 ).

Auxiliary Space will be O(N).

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience