- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count of strings satisfying the given conditions
Introduction
A string in C++ is also a primitive data type which is comprised of alphanumeric characters. The letters in a string are case sensitive, but strings can contain both upper and lower case.
In this article, we are given an input array of lowercase strings, and require the count of the pair of strings from the array satisfying the following conditions −
Both the strings should have the same first and last vowel
Both the strings have an equal number of pairs
An array is a data structure simulating the storage of similar elements. A C++ array must satisfy the following properties −
All the elements in an array must belong to the same data type
An array is associated with a fixed length
Here, is an example to get an idea about the asked question,
Sample Example
Example 1 - arr : { “bitch” , “glitch” , “bitter”, “ditch”}
Output - 1
Explanation - The pair of strings satisfying the conditions are “bitch” and “glitch” which have the vowel {i}. The first and last vowel in these strings are also ‘i’ which are equivalent. The string “ditch” also satisfies the condition, but it has already been included in a valid pair.
The method used for solving this problem is character checking and then maintaining a tuple in C++ to store the counts of vowels.
Syntax
make_tuple ( val1, val2, val3..)
The make_tuple() function in C++ is used to construct the objects of specified types of the tuple. It is used to assign values to a tuple in the map. The values are assigned in the order of their declaration.
Parameters
val1, val2 , .. - The values to be assigned to tuple in the map.
Another method used during the evaluation of this approach is −
push_back( val1)
The push_back() in-built method is used to insert elements into C++ data objects, like vectors or maps. The insertion always takes place from the end. Upon every insertion the size of the object increases by one. The method gets executed in constant time complexity.
Parameters
val - The values to be inserted to the C++ object.
Algorithm
A sample string array, arr is taken as input
A map map is maintained to store the tuples of words satisfying the required conditions
An iteration over the arr is performed using a for loop, i
- During every iteration, a vector of characters, vec is stored to keep a track on the extracted vowels of the word being taken from the array, arr
The particular word of the array, is referenced using the word variable.
Another loop iteration, j is performed over the extracted array word. Every time, the particular character is extracted from the word, and checked if it is a vowel or a consonant
In case, the extracted character is vowel, it is pushed in vec.
In case, the vec is non empty, that is there are vowels, the first and last vowel is extracted from the current word i.
A tuple is constructed in the map using the extracted vowels and the total number of vowels in the ith word and then mapping it to the ith index. The created tuple is then added to the end of the map using the push_back() method.
An iteration of the map is performed
The pairs that are possible to be formed up using the tuple pairs in the map are counted.
The count of pairs is then returned as the output, reflected by the count variable.
Example
The following C++ code snippet illustrates the process of counting the strings satisfying the following conditions −
//include the required libraries #include <bits/stdc++.h> using namespace std; //count number of pairs satisfying the cond int satisfycondn(string sarr[], int n){ //declaring map to store tuples map<tuple<char, char, int>, vector<int> > map; // For every string of the array for (int i = 0; i < n; i++) { //storing vowels of the extracted word vector<char> vec; //extracting the word string word = sarr[i]; for (int j = 0; j < word.size(); j++) { char ch = sarr[i][j]; //checking if the character is vowel if (ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u') vec.push_back(ch); } int numvowels = vec.size(); // If current string contains vowels if (numvowels > 0) { int frstvowel = vec[0]; int scndvowel = vec[numvowels - 1]; map[make_tuple(frstvowel, scndvowel, numvowels)] .push_back(i); } } //maintaining a count to store the pair of strings satisfying the cond int count = 0; //iterating over the map for (auto m : map) { vector<int> v = m.second; //getting the valid pair of size int v_size = v.size(); //incrementing the counter count += v_size / 2; } return count; } int main() { //declaring a sample array string arr[] = { "point", "coin","groin","foul","area", "mourn" }; int n = sizeof(arr) / sizeof(string); cout << "Count of strings satisfying the condition : "<<satisfycondn(arr,n); return 0; }
Output
Count of strings satisfying the condition − 2
Explanation − There are two pairs of strings in the array satisfying the condition − {point, coin} and { foul, mourn”}
The first pair of strings have vowels 2 each and are o , i respectively.
The second pair of strings have 2 vowels each and are o , u respectively.
Conclusion
Maps in C++ are quite versatile data structures used to store data and their associated attributes in a much friendlier way. It simplifies the access of the data according to the specified conditions.