Open In App

# Count pairs of strings that satisfy the given conditions

Given an array arr[] of N strings consisting of lowercase characters, the task is to count the pairs in the array which satisfy the given conditions:

1. Both strings have an equal number of pairs.
2. The first vowels of both the strings are same.
3. The last vowel of both strings is the same.

Note that a string can only be used in a single pair. Examples:

Input: arr[] = {“geeks”, “for”, “geeks”, “geek”} Output: 1 The only valid pair is (“geeks”, “geeks”). “geek” could also be paired with “geeks” but both the “geeks” have already been paired. Input: arr[] = {“code”, “shoot”, “mode”} Output: 1

Approach: We will store all the vowels that appear in a word for each word and we make a tuple of first vowel, last vowel and the total count of vowels and store corresponding index regarding that tuple using map. At last, we will go through the map and count number of pairs that can be formed using the tuple values stored in the map. Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach``#include ``using` `namespace` `std;` `// Function that returns true if c is vowel``bool` `is_vowel(``char` `c)``{``    ``return` `(c == ``'a'` `|| c == ``'e'` `|| c == ``'i'``            ``|| c == ``'o'` `|| c == ``'u'``);``}` `// Function to return the count of required pairs``int` `count(string s[], ``int` `n)``{` `    ``map, vector<``int``> > map;` `    ``// For every string of the array``    ``for` `(``int` `i = 0; i < n; i++) {` `        ``// Vector to store the vowels``        ``// of the current string``        ``vector<``char``> vowel;``        ``for` `(``int` `j = 0; j < s[i].size(); j++) {` `            ``// If current character is a vowel``            ``if` `(is_vowel(s[i][j]))``                ``vowel.push_back(s[i][j]);``        ``}` `        ``// If current string contains vowels``        ``if` `(vowel.size() > 0) {``            ``int` `len = vowel.size();` `            ``// Create tuple (first vowel,``            ``// last vowel, total vowels)``            ``map[make_tuple(vowel[0],``                           ``vowel[len - 1], len)]``                ``.push_back(i);``        ``}``    ``}` `    ``int` `count = 0;``    ``for` `(``auto` `i : map) {` `        ``// v stores the indices for which``        ``// the given condition satisfies``        ``// Total valid pairs will be half the size``        ``vector<``int``> v = i.second;``        ``count += v.size() / 2;``    ``}` `    ``return` `count;``}` `// Driver code``int` `main()``{``    ``string s[] = { ``"geeks"``, ``"for"``, ``"geeks"` `};``    ``int` `n = ``sizeof``(s) / ``sizeof``(string);` `    ``cout << count(s, n);` `    ``return` `0;``}`

## Java

 `import` `java.util.*;` `class` `GFG``{``  ` `  ``// Returns true when it is vowel``  ``static` `boolean` `is_vowel(``char` `c)``  ``{``    ``return` `(c == ``'a'` `|| c == ``'e'` `|| c == ``'i'` `|| c == ``'o'``            ``|| c == ``'u'``);``  ``}` `  ``// Function to return the count of required pairs``  ``static` `int` `count(String s[], ``int` `n)``  ``{``    ``Map > map = ``new` `HashMap<>();` `    ``for` `(``int` `i = ``0``; i < n; i++) {``      ``List vowels = ``new` `ArrayList<>();``      ``for` `(``int` `j = ``0``; j < s[i].length(); j++) {``        ``if` `(is_vowel(s[i].charAt(j))) {``          ``vowels.add(s[i].charAt(j));``        ``}``      ``}` `      ``if` `(vowels.size() > ``0``) {``        ``int` `len = vowels.size();``        ``String key = vowels.get(``0``) + ``""``          ``+ vowels.get(len - ``1``) + ``""``          ``+ len;``        ``if` `(!map.containsKey(key)) {``          ``map.put(key, ``new` `ArrayList());``        ``}``        ``map.get(key).add(i);``      ``}``    ``}` `    ``int` `count = ``0``;``    ``for` `(List value : map.values())``    ``{` `      ``// value stores the indices for which``      ``// the given condition satisfies``      ``// Total valid pairs will be half the size``      ``count += value.size() / ``2``;``    ``}` `    ``return` `count;``  ``}` `  ``public` `static` `void` `main(String[] args)``  ``{``    ``String s[] = { ``"geeks"``, ``"for"``, ``"geeks"` `};``    ``int` `n = s.length;` `    ``System.out.println(count(s, n));``  ``}``}` `// This code is contributed by abn95knd1.`

## Python3

 `# Python3 implementation of the approach` `# Function that returns true if c is vowel``def` `is_vowel(c):``    ``return` `(c ``=``=` `'a'` `or` `c ``=``=` `'e'` `or` `c ``=``=` `'i'``            ``or` `c ``=``=` `'o'` `or` `c ``=``=` `'u'``)`  `# Function to return the count of required pairs``def` `count(s, n):`  `    ``map``=``dict``()` `    ``# For every of the array``    ``for` `i ``in` `range``(n):` `        ``# Vector to store the vowels``        ``# of the current string``        ``vowel``=``[]``        ``for` `j ``in` `range``(``len``(s[i])):` `            ``# If current character is a vowel``            ``if` `(is_vowel(s[i][j])):``                ``vowel.append(s[i][j])``    `  `        ``# If current contains vowels``        ``if` `(``len``(vowel) > ``0``):``            ``Len` `=` `len``(vowel)` `            ``# Create tuple (first vowel,``            ``# last vowel, total vowels)``            ``if` `(vowel[``0``],vowel[``Len` `-` `1``], ``Len``) ``in` `map``.keys():``                ``map``[(vowel[``0``],vowel[``Len` `-` `1``], ``Len``)].append(i)``            ``else``:``                ``map``[(vowel[``0``],vowel[``Len` `-` `1``], ``Len``)]``=``[i,]``        `  `    ``count ``=` `0``    ``for` `i ``in` `map``:` `        ``# v stores the indices for which``        ``# the given condition satisfies``        ``# Total valid pairs will be half the size``        ``v ``=` `map``[i]``        ``count ``+``=` `len``(v)``/``/` `2``    `  `    ``return` `count` `# Driver code``s ``=` `[``"geeks"``, ``"for"``, ``"geeks"``]``n ``=` `len``(s)` `print``(count(s, n))` `# This code is contributed by mohit kumar 29`

## Javascript

 `// Returns true when it is vowel``function` `is_vowel(c) {``  ``return` `(c === ``'a'` `|| c === ``'e'` `|| c === ``'i'` `|| c === ``'o'` `|| c === ``'u'``);``}` `// Function to return the count of required pairs``function` `count(s, n) {``  ``const map = ``new` `Map();` `  ``for` `(let i = 0; i < n; i++) {``    ``const vowels = [];``    ``for` `(let j = 0; j < s[i].length; j++) {``      ``if` `(is_vowel(s[i].charAt(j))) {``        ``vowels.push(s[i].charAt(j));``      ``}``    ``}` `    ``if` `(vowels.length > 0) {``      ``const len = vowels.length;``      ``const key = vowels[0] + ``""` `+ vowels[len - 1] + ``""` `+ len;``      ``if` `(!map.has(key)) {``        ``map.set(key, []);``      ``}``      ``map.get(key).push(i);``    ``}``  ``}` `  ``let count = 0;``  ``for` `(const value of map.values()) {``  ` `    ``// value stores the indices for which``    ``// the given condition satisfies``    ``// Total valid pairs will be half the size``    ``count += Math.floor(value.length / 2);``  ``}` `  ``return` `count;``}` `const s = [``"geeks"``, ``"for"``, ``"geeks"``];``const n = s.length;` `console.log(count(s, n));`

## C#

 `using` `System;``using` `System.Collections.Generic;` `class` `GFG``{``// Returns true when it is vowel``static` `bool` `IsVowel(``char` `c)``{``return` `(c == ``'a'` `|| c == ``'e'` `|| c == ``'i'` `|| c == ``'o'` `|| c == ``'u'``);``}``// Function to return the count of required pairs``static` `int` `Count(``string``[] s, ``int` `n)``{``    ``Dictionary<``string``, List<``int``>> map = ``new` `Dictionary<``string``, List<``int``>>();` `    ``for` `(``int` `i = 0; i < n; i++)``    ``{``        ``List<``char``> vowels = ``new` `List<``char``>();``        ``for` `(``int` `j = 0; j < s[i].Length; j++)``        ``{``            ``if` `(IsVowel(s[i][j]))``            ``{``                ``vowels.Add(s[i][j]);``            ``}``        ``}` `        ``if` `(vowels.Count > 0)``        ``{``            ``int` `len = vowels.Count;``            ``string` `key = vowels[0] + ``""` `+ vowels[len - 1] + ``""` `+ len;``            ``if` `(!map.ContainsKey(key))``            ``{``                ``map.Add(key, ``new` `List<``int``>());``            ``}``            ``map[key].Add(i);``        ``}``    ``}` `    ``int` `count = 0;``    ``foreach` `(List<``int``> value ``in` `map.Values)``    ``{``        ``// value stores the indices for which``        ``// the given condition satisfies``        ``// Total valid pairs will be half the size``        ``count += value.Count / 2;``    ``}` `    ``return` `count;``}` `static` `void` `Main(``string``[] args)``{``    ``string``[] s = { ``"geeks"``, ``"for"``, ``"geeks"` `};``    ``int` `n = s.Length;` `    ``Console.WriteLine(Count(s, n));``}``}`

Output

`1`

Time Complexity: O(n * l), where n is the length of str array and l is the maximum length of a string in the array.
Auxiliary Space: O(n), where n is the length of the given string.