📌  相关文章
📜  给定字符串中有效对(X,Y)的计数,以便将X与自身连接起来可得出Y

📅  最后修改于: 2021-04-29 05:50:34             🧑  作者: Mango

给定N个字符串的数组arr [] 。设XY为两个字符串, XY称为 如果从XX的串联(即X + X )重新排列结果字符串得到Y 则为有效对。任务是计算此类有效对的数量。

例子:

天真的方法:想法是生成所有可能的对,并根据给定条件检查是否有任何对构成有效对。如果是,则计算该对并检查下一对。完成上述步骤后,打印计数值。

时间复杂度: O(N 2 )
辅助空间: O(1)

高效的方法:想法是将排序后的字符串及其计数存储在Hashmap中,并遍历数组中的每个字符串,将其与其自身连接起来,然后在Hashmap中查找其计数,将其添加到对数中。步骤如下:

  1. 创建一个哈希图。
  2. 对数组中给定的字符串进行排序,并将其计数存储在哈希图中。
  3. 再次迭代通过所有字符串并连接各字符串本身的排序字符串,并找到其在哈希映射计数。
  4. 在上述步骤中更新最终计数,并在上述所有步骤之后打印最终计数。

下面是上述方法的实现:

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


输出:
1



时间复杂度: O(N)
辅助空间: O(N)