给定一个由N 个字符串的数组arr[] 。设X和Y为两个字符串,则称X和Y为 有效对如果所得字符串的从X的级联的重排用X(即,X + X)给出ÿ。任务是计算此类有效对的数量。
例子:
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.
朴素的方法:这个想法是生成所有可能的对,并检查是否有任何对根据给定的条件形成有效对。如果是,则计算这一对并检查下一对。完成上述步骤后打印count的值。
时间复杂度: O(N 2 )
辅助空间: O(1)
有效的方法:想法是将排序后的字符串与其计数一起存储在 Hashmap 中,并遍历数组中的每个字符串,将它与自身连接起来,并在Hashmap 中找到它的计数,并将其添加到对的计数中。以下是步骤:
- 创建一个哈希图。
- 对数组中的给定字符串进行排序,并将它们的计数存储在 hashmap 中。
- 再次迭代通过所有字符串并连接各字符串本身的排序字符串,并找到其在哈希映射计数。
- 更新上述步骤中的最终计数,并在完成上述所有步骤后打印最终计数。
下面是上述方法的实现:
C++14
// C++14 program for the above approach
#include
using namespace std;
// Function to implement sorting on
// strings
string sorted(string s)
{
// Convert string to char array
char ch[s.length()];
for(int i = 0; i < s.length(); i++)
{
ch[i] = s[i];
}
// Sort the array
sort(ch, ch + s.length());
string sb;
for(char c : ch)
sb += c;
// Return string
return sb;
}
// Function that count total number
// of valid pairs
int countPairs(string arr[], int N)
{
// Create hashmap to store the
// frequency of each string
// in sorted form
map mp;
// Initialise the count of pairs
int count = 0;
for(int i = 0; i < N; i++)
{
// Store each string in sorted
// form along with it's count
string s = sorted(arr[i]);
mp[s]++;
}
// Iterate through each string
// in the array
for(int i = 0; i < N; i++)
{
// Concatenate each string with itself
arr[i] = arr[i] + arr[i];
sorted(arr[i]);
// Find its count in the hashmap
count += mp[sorted(arr[i])];
}
// Return answer
return count;
}
// Driver Code
int main()
{
int N = 3;
// Given array of strings
string arr[] = { "easy", "yeasseay",
"medium" };
// Function Call
cout << countPairs(arr, N) << endl;
return 0;
}
// This code is contributed by sallagondaavinashreddy7
Java
// Java program for the above approach
import java.util.*;
public class Main {
// Function that count total number
// of valid pairs
public static int
countPairs(String arr[], int N)
{
// Create hashmap to store the
// frequency of each string
// in sorted form
HashMap map
= new HashMap<>();
// Initialise the count of pairs
int count = 0;
for (int i = 0; i < N; i++) {
String s = sort(arr[i]);
// Store each string in sorted
// form along with it's count
map.put(s, map.getOrDefault(s, 0) + 1);
}
// Iterate through each string
// in the array
for (int i = 0; i < N; i++) {
// Concatenate each string with itself
String s = sort(arr[i] + arr[i]);
// Find its count in the hashmap
count += map.getOrDefault(s, 0);
}
// Return answer
return count;
}
// Function to implement sorting on
// strings
public static String sort(String s)
{
// Convert string to char array
char ch[] = s.toCharArray();
// Sort the array
Arrays.sort(ch);
StringBuffer sb = new StringBuffer();
for (char c : ch)
sb.append(c);
// Return string
return sb.toString();
}
// Driver Code
public static void main(String args[])
{
int N = 3;
// Given array of strings
String arr[] = { "easy", "yeasseay",
"medium" };
// Function Call
System.out.println(countPairs(arr, N));
}
}
Python3
# Python3 program for the above approach
from collections import defaultdict
# Function that count total number
# of valid pairs
def countPairs(arr, N):
# Create hashmap to store the
# frequency of each string
# in sorted form
map = defaultdict(lambda : 0)
# Initialise the count of pairs
count = 0
for i in range(N):
s = sorted(arr[i])
# Store each string in sorted
# form along with it's count
map["".join(s)] += 1
# Iterate through each string
# in the array
for i in range(N):
# Concatenate each string with itself
s = sorted(arr[i] + arr[i])
# Find its count in the hashmap
count += map["".join(s)]
# Return answer
return count
# Driver Code
N = 3
# Given array of strings
arr = [ "easy", "yeasseay", "medium" ]
# Function call
print(countPairs(arr, N))
# This code is contributed by Shivam Singh
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
using System.Text;
class GFG{
// Function that count total number
// of valid pairs
public static int countPairs(string []arr, int N)
{
// Create hashmap to store the
// frequency of each string
// in sorted form
Dictionary map = new Dictionary();
// Initialise the count of pairs
int count = 0;
for(int i = 0; i < N; i++)
{
string s = sort(arr[i]);
// Store each string in sorted
// form along with it's count
if (map.ContainsKey(s))
{
map[s]++;
}
else
{
map[s] = 1;
}
}
// Iterate through each string
// in the array
for(int i = 0; i < N; i++)
{
// Concatenate each string with itself
string s = sort(arr[i] + arr[i]);
// Find its count in the hashmap
count += map.GetValueOrDefault(s, 0);
}
// Return answer
return count;
}
// Function to implement sorting on
// strings
public static string sort(string s)
{
// Convert string to char array
char []ch = s.ToCharArray();
// Sort the array
Array.Sort(ch);
StringBuilder sb = new StringBuilder();
foreach(char c in ch)
sb.Append(c);
// Return string
return sb.ToString();
}
// Driver Code
public static void Main(string []args)
{
int N = 3;
// Given array of strings
string []arr = { "easy", "yeasseay",
"medium" };
// Function call
Console.Write(countPairs(arr, N));
}
}
// This code is contributed by rutvik_56
Javascript
1
时间复杂度: O(N)
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live