Print all Substrings of a String that has equal number of vowels and consonants
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.
C++
// C++ program for the approach #include <bits/stdc++.h> using namespace std; // Function to check if character is // vowel or consonant. bool isVowel( char c) { return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' ; } // Function to print all // possible substring void all_substring(vector<string>& allsubstrings) { // length of the vector containing all substrings int n = allsubstrings.size(); for ( int i = 0; i < n; i++) { // storing length of each substring generated int m = allsubstrings[i].size(); // count variable for vowel and consonant characters int cnt_vowel = 0, cnt_consonant = 0; // checking if count of vowels and consonants same // or not for ( int j = 0; j < m; j++) { if (isVowel(allsubstrings[i][j])) cnt_vowel++; else cnt_consonant++; } if (cnt_vowel == cnt_consonant) cout << allsubstrings[i] << endl; } } // Function to print all sub strings vector<string> subString(string& str) { // length of the input string int n = str.size(); // resultant vector storing all substrings vector<string> res; // Pick starting point for ( int len = 1; len <= n; len++) { // Pick ending point for ( int i = 0; i <= n - len; i++) { // Print characters from current // starting point to current ending // point. int j = i + len - 1; // storing each substring using tmp string tmp = "" ; for ( int k = i; k <= j; k++) tmp += str[k]; res.push_back(tmp); } } return res; } // Driver code int main() { // Input string string str = "geeks" ; // Function Call vector<string> allsubstrings = subString(str); // Function Call all_substring(allsubstrings); return 0; } |
Java
// Java program for the approach import java.util.*; public class SubstringVowelConsonant { // Function to check if character is // vowel or consonant. static boolean isVowel( char c) { return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' ; } // Function to print all // possible substring static void all_substring(List<String> allsubstrings) { // length of the vector containing all substrings int n = allsubstrings.size(); for ( int i = 0 ; i < n; i++) { // storing length of each substring generated int m = allsubstrings.get(i).length(); // count variable for vowel and consonant // characters int cnt_vowel = 0 , cnt_consonant = 0 ; // checking if count of vowels and consonants // same or not for ( int j = 0 ; j < m; j++) { if (isVowel(allsubstrings.get(i).charAt(j))) cnt_vowel++; else cnt_consonant++; } if (cnt_vowel == cnt_consonant) System.out.println(allsubstrings.get(i)); } } // Function to print all sub strings static List<String> subString(String str) { // length of the input string int n = str.length(); // resultant vector storing all substrings List<String> res = new ArrayList<>(); // Pick starting point for ( int len = 1 ; len <= n; len++) { // Pick ending point for ( int i = 0 ; i <= n - len; i++) { // Print characters from current // starting point to current ending // point. int j = i + len - 1 ; // storing each substring using tmp String tmp = "" ; for ( int k = i; k <= j; k++) tmp += str.charAt(k); res.add(tmp); } } return res; } // Driver code public static void main(String[] args) { // Input string String str = "geeks" ; // Function Call List<String> allsubstrings = subString(str); // Function Call all_substring(allsubstrings); } } |
Python3
# Python3 program for the approach # Function to check if character is # vowel or consonant. def isVowel(c): return c = = 'a' or c = = 'e' or c = = 'i' or c = = 'o' or c = = 'u' # Function to print all possible substring def all_substring(allsubstrings): # length of the vector containing all substrings n = len (allsubstrings) for i in range (n): # storing length of each substring generated m = len (allsubstrings[i]) # count variable for vowel and consonant characters cnt_vowel = 0 cnt_consonant = 0 # checking if count of vowels and consonants same # or not for j in range (m): if (isVowel(allsubstrings[i][j])): cnt_vowel + = 1 else : cnt_consonant + = 1 if (cnt_vowel = = cnt_consonant): print (allsubstrings[i]) # Function to print all sub strings def subString(s): # length of the input string n = len (s) # resultant list storing all substrings res = [] # Pick starting point for i in range (n): # Pick ending point for j in range (i + 1 , n + 1 ): # storing each substring using tmp tmp = s[i:j] res.append(tmp) return res # Driver code if __name__ = = '__main__' : # Input string str = "geeks" # Function Call allsubstrings = subString( str ) # Function Call all_substring(allsubstrings) |
C#
// C# program for the approach using System; using System.Collections.Generic; using System.Linq; class GFG { // Function to check if character is // vowel or consonant. static bool IsVowel( char c) { return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' ; } // Function to print all // possible substring static void AllSubstring(List< string > allsubstrings) { // length of the vector containing all substrings int n = allsubstrings.Count(); for ( int i = 0; i < n; i++) { // storing length of each substring generated int m = allsubstrings[i].Length; // count variable for vowel and consonant // characters int cnt_vowel = 0, cnt_consonant = 0; // checking if count of vowels and consonants // same or not for ( int j = 0; j < m; j++) { if (IsVowel(allsubstrings[i][j])) cnt_vowel++; else cnt_consonant++; } if (cnt_vowel == cnt_consonant) Console.WriteLine(allsubstrings[i]); } } // Function to print all sub strings static List< string > SubString( string str) { // length of the input string int n = str.Length; // resultant list storing all substrings List< string > res = new List< string >(); // Pick starting point for ( int len = 1; len <= n; len++) { // Pick ending point for ( int i = 0; i <= n - len; i++) { // Print characters from current // starting point to current ending // point. int j = i + len - 1; // storing each substring using tmp string tmp = "" ; for ( int k = i; k <= j; k++) tmp += str[k]; res.Add(tmp); } } return res; } // Driver code static void Main() { // Input string string str = "geeks" ; // Function Call List< string > allsubstrings = SubString(str); // Function Call AllSubstring(allsubstrings); } } |
Javascript
// Function to check if character is vowel or consonant. function isVowel(c) { return c === 'a' || c === 'e' || c === 'i' || c === 'o' || c === 'u' ; } // Function to print all possible substrings function all_substrings(allsubstrings) { // Length of the vector containing all substrings let n = allsubstrings.length; for (let i = 0; i < n; i++) { // Storing length of each substring generated let m = allsubstrings[i].length; // Count variable for vowel and consonant characters let cnt_vowel = 0, cnt_consonant = 0; // Checking if count of vowels and consonants are the same or not for (let j = 0; j < m; j++) { if (isVowel(allsubstrings[i][j])) cnt_vowel++; else cnt_consonant++; } if (cnt_vowel === cnt_consonant) console.log(allsubstrings[i]); } } // Function to print all substrings function subString(str) { // Length of the input string let n = str.length; // Resultant array storing all substrings let res = []; // Pick starting point for (let len = 1; len <= n; len++) { // Pick ending point for (let i = 0; i <= n - len; i++) { // Print characters from current // starting point to current ending // point. let j = i + len - 1; // Storing each substring using tmp let tmp = "" ; for (let k = i; k <= j; k++) tmp += str[k]; res.push(tmp); } } return res; } // Driver code let str = "geeks" ; // Input string // Function Call let allsubstrings = subString(str); // Function Call all_substrings(allsubstrings); |
ge ek geek eeks
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 consonant. 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 consonant 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<Pair> 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<Tuple< int , int > > ans= new List<Tuple< int , int >>(); // 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( "</br>" ); } } // Driver Code let s = "geeks" ; let n = s.length; // Function call all_substring(s, n); |
ge geek eeks ek
Time Complexity: O(N2)
Auxiliary Space: O(N)
Please Login to comment...