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:
- Both strings have an equal number of pairs.
- The first vowels of both the strings are same.
- 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++
#include <bits/stdc++.h>
using namespace std;
bool is_vowel( char c)
{
return (c == 'a' || c == 'e' || c == 'i'
|| c == 'o' || c == 'u' );
}
int count(string s[], int n)
{
map<tuple< char , char , int >, vector< int > > map;
for ( int i = 0; i < n; i++) {
vector< char > vowel;
for ( int j = 0; j < s[i].size(); j++) {
if (is_vowel(s[i][j]))
vowel.push_back(s[i][j]);
}
if (vowel.size() > 0) {
int len = vowel.size();
map[make_tuple(vowel[0],
vowel[len - 1], len)]
.push_back(i);
}
}
int count = 0;
for ( auto i : map) {
vector< int > v = i.second;
count += v.size() / 2;
}
return count;
}
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
{
static boolean is_vowel( char c)
{
return (c == 'a' || c == 'e' || c == 'i' || c == 'o'
|| c == 'u' );
}
static int count(String s[], int n)
{
Map<String, List<Integer> > map = new HashMap<>();
for ( int i = 0 ; i < n; i++) {
List<Character> 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<Integer>());
}
map.get(key).add(i);
}
}
int count = 0 ;
for (List<Integer> value : map.values())
{
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));
}
}
|
Python3
def is_vowel(c):
return (c = = 'a' or c = = 'e' or c = = 'i'
or c = = 'o' or c = = 'u' )
def count(s, n):
map = dict ()
for i in range (n):
vowel = []
for j in range ( len (s[i])):
if (is_vowel(s[i][j])):
vowel.append(s[i][j])
if ( len (vowel) > 0 ):
Len = len (vowel)
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 = map [i]
count + = len (v) / / 2
return count
s = [ "geeks" , "for" , "geeks" ]
n = len (s)
print (count(s, n))
|
Javascript
function is_vowel(c) {
return (c === 'a' || c === 'e' || c === 'i' || c === 'o' || c === 'u' );
}
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()) {
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
{
static bool IsVowel( char c)
{
return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' );
}
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)
{
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));
}
}
|
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.