Count of valid pairs (X, Y) from given strings such that concatenating X with itself yields Y
Last Updated :
31 May, 2021
Given an array arr[] of N strings. Let X and Y be two strings, X and Y are said to be valid pairs if the rearrangement of the resultant string from the concatenation of X with X (i.e., X+X) gives Y. The task is to count the number of such valid pairs.
Examples:
Input: N = 4, arr[] = {“hacker”, ”ackerhackerh”, ”int”, ”iittnn”, ”long”}
Output: 2
Explanation:
Pair {“hacker”, ”ackerhackerh”} “hacker” When concatenated with “hacker” gives ”ackerhackerh” after rearrangement.
Pair {“int”, ”iittnn”} “int” When concatenated with “int” gives ”iittnn” after rearrangement.
Input: N = 3, arr[] = {“easy”, ”yeasseay“, “medium“}
Output:1
Explanation:
Pair {“easy”, ”yeasseay“} “easy” When concatenated with “easy” gives ”yeasseay“ after rearrangement.
Naive Approach: The idea is to generate all possible pairs and check if any pairs form a valid pair as per the given condition or not. If yes then count this pair and check for the next pair. Print the value of count after the above steps.
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: The idea is to store the sorted string in Hashmap along with its count and iterate through each string of the array concatenate it with itself and find its count in Hashmap add it to the count of pairs. Below are the steps:
- Create a hashmap.
- Sort the given strings in the array and store their count in hashmap.
- Again iterate through all strings and concatenate each string with itself sort the string and find its count in Hashmap.
- Update the final count in the above step and print the final count after all the above steps.
Below is the implementation of the above approach:
C++14
#include<bits/stdc++.h>
using namespace std;
string sorted(string s)
{
char ch[s.length()];
for ( int i = 0; i < s.length(); i++)
{
ch[i] = s[i];
}
sort(ch, ch + s.length());
string sb;
for ( char c : ch)
sb += c;
return sb;
}
int countPairs(string arr[], int N)
{
map<string, int > mp;
int count = 0;
for ( int i = 0; i < N; i++)
{
string s = sorted(arr[i]);
mp[s]++;
}
for ( int i = 0; i < N; i++)
{
arr[i] = arr[i] + arr[i];
sorted(arr[i]);
count += mp[sorted(arr[i])];
}
return count;
}
int main()
{
int N = 3;
string arr[] = { "easy" , "yeasseay" ,
"medium" };
cout << countPairs(arr, N) << endl;
return 0;
}
|
Java
import java.util.*;
public class Main {
public static int
countPairs(String arr[], int N)
{
HashMap<String, Integer> map
= new HashMap<>();
int count = 0 ;
for ( int i = 0 ; i < N; i++) {
String s = sort(arr[i]);
map.put(s, map.getOrDefault(s, 0 ) + 1 );
}
for ( int i = 0 ; i < N; i++) {
String s = sort(arr[i] + arr[i]);
count += map.getOrDefault(s, 0 );
}
return count;
}
public static String sort(String s)
{
char ch[] = s.toCharArray();
Arrays.sort(ch);
StringBuffer sb = new StringBuffer();
for ( char c : ch)
sb.append(c);
return sb.toString();
}
public static void main(String args[])
{
int N = 3 ;
String arr[] = { "easy" , "yeasseay" ,
"medium" };
System.out.println(countPairs(arr, N));
}
}
|
Python3
from collections import defaultdict
def countPairs(arr, N):
map = defaultdict( lambda : 0 )
count = 0
for i in range (N):
s = sorted (arr[i])
map ["".join(s)] + = 1
for i in range (N):
s = sorted (arr[i] + arr[i])
count + = map ["".join(s)]
return count
N = 3
arr = [ "easy" , "yeasseay" , "medium" ]
print (countPairs(arr, N))
|
C#
using System;
using System.Collections.Generic;
using System.Text;
class GFG{
public static int countPairs( string []arr, int N)
{
Dictionary< string ,
int > map = new Dictionary< string ,
int >();
int count = 0;
for ( int i = 0; i < N; i++)
{
string s = sort(arr[i]);
if (map.ContainsKey(s))
{
map[s]++;
}
else
{
map[s] = 1;
}
}
for ( int i = 0; i < N; i++)
{
string s = sort(arr[i] + arr[i]);
count += map.GetValueOrDefault(s, 0);
}
return count;
}
public static string sort( string s)
{
char []ch = s.ToCharArray();
Array.Sort(ch);
StringBuilder sb = new StringBuilder();
foreach ( char c in ch)
sb.Append(c);
return sb.ToString();
}
public static void Main( string []args)
{
int N = 3;
string []arr = { "easy" , "yeasseay" ,
"medium" };
Console.Write(countPairs(arr, N));
}
}
|
Javascript
<script>
function sorted(s)
{
var ch = Array(s.length);
for ( var i = 0; i < s.length; i++)
{
ch[i] = s[i];
}
ch.sort();
var sb;
ch.forEach(c => {
sb += c;
});
return sb;
}
function countPairs( arr, N)
{
var mp = new Map();
var count = 0;
for ( var i = 0; i < N; i++)
{
var s = sorted(arr[i]);
if (mp.has(s))
mp.set(s, mp.get(s)+1)
else
mp.set(s, 1)
}
for ( var i = 0; i < N; i++)
{
arr[i] = arr[i] + arr[i];
sorted(arr[i]);
count += mp.has(sorted(arr[i]))?mp.get(sorted(arr[i])):0;
}
return count;
}
var N = 3;
var arr = [ "easy" , "yeasseay" ,
"medium" ];
document.write( countPairs(arr, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...