Techno Blender
Digitally Yours.

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

0 35


Improve Article

Save Article

Like Article

Improve Article

Save Article

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 <bits/stdc++.h>
using namespace std;

// Initialising vector
vector<pair<int, int> > 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;
}

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


Improve Article

Save Article

Like Article

Improve Article

Save Article

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 <bits/stdc++.h>
using namespace std;

// Initialising vector
vector<pair<int, int> > 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;
}

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

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.
Leave a comment