📌  相关文章
📜  计算字符串中相等对的数量

📅  最后修改于: 2021-05-07 07:20:02             🧑  作者: Mango

给定字符串s,找到相同的字符对数。对(s [i],s [j]),(s [j],s [i]),(s [i],s [i]),(s [j],s [j])对应为被认为是不同的。

例子 :

Input: air
Output: 3
Explanation :
3 pairs that are equal are (a, a), (i, i) and (r, r)

Input : geeksforgeeks
Output : 31

天真的方法是您运行两个嵌套的for循环并找出所有对,并保留所有对的计数。但这对于较长的字符串长度来说不够有效。

对于一种有效的方法,我们需要计算线性时间中相等对的数量。由于对(x,y)和对(y,x)被认为是不同的。我们需要使用一个哈希表来存储一个字符所有出现的次数。因此,我们知道一个字符如果出现两次,那么它将有4对- (i,i),(j,j),(i,j ),(j,i) 。因此,使用哈希函数,存储每个字符的出现次数,然后每个字符的对数将为^ 2。哈希表的长度为256,因为我们有256个字符。

下面是上述方法的实现:

C++
// CPP program to count the number of pairs
#include 
using namespace std;
#define MAX 256
  
// Function to count the number of equal pairs
int countPairs(string s)
{
    // Hash table
    int cnt[MAX] = { 0 };
  
    // Traverse the string and count occurrence
    for (int i = 0; i < s.length(); i++)
        cnt[s[i]]++;
  
    // Stores the answer
    int ans = 0;
  
    // Traverse and check the occurrence of every character
    for (int i = 0; i < MAX; i++)
        ans += cnt[i] * cnt[i];
  
    return ans;
}
  
// Driver Code
int main()
{
    string s = "geeksforgeeks";
    cout << countPairs(s);
    return 0;
}


Java
// Java program to count the number of pairs
import java.io.*;
  
class GFG {
  
    static int MAX = 256;
      
    // Function to count the number of equal pairs
    static int countPairs(String s)
    {
        // Hash table
        int cnt[] = new int[MAX];
      
        // Traverse the string and count occurrence
        for (int i = 0; i < s.length(); i++)
            cnt[s.charAt(i)]++;
      
        // Stores the answer
        int ans = 0;
      
        // Traverse and check the occurrence
        // of every character
        for (int i = 0; i < MAX; i++)
            ans += cnt[i] * cnt[i];
      
        return ans;
    }
      
    // Driver Code
    public static void main (String[] args)
    {
        String s = "geeksforgeeks";
        System.out.println(countPairs(s));
    }
}
  
// This code is contributed by vt_m


Python 3
# Python3 program to count the 
# number of pairs 
MAX = 256
  
# Function to count the number 
# of equal pairs
def countPairs(s):
      
    # Hash table 
    cnt = [0 for i in range(0, MAX)]
  
    # Traverse the string and count 
    # occurrence 
    for i in range(len(s)):
        cnt[ord(s[i]) - 97] += 1
  
    # Stores the answer 
    ans = 0
  
    # Traverse and check the occurrence 
    # of every character 
    for i in range(0, MAX):
        ans += cnt[i] * cnt[i]
  
    return ans
  
# Driver code 
if __name__=="__main__":
    s = "geeksforgeeks"
    print(countPairs(s))
  
# This code is contributed 
# by Sairahul099


C#
// C# program to count the number of pairs
using System;
  
class GFG {
  
    static int MAX = 256;
      
    // Function to count the number of equal pairs
    static int countPairs(string s)
    {
        // Hash table
        int []cnt = new int[MAX];
      
        // Traverse the string and count occurrence
        for (int i = 0; i < s.Length; i++)
            cnt[s[i]]++;
      
        // Stores the answer
        int ans = 0;
      
        // Traverse and check the occurrence
        // of every character
        for (int i = 0; i < MAX; i++)
            ans += cnt[i] * cnt[i];
      
        return ans;
    }
      
    // Driver Code
    public static void Main ()
    {
        string s = "geeksforgeeks";
        Console.WriteLine(countPairs(s));
    }
}
  
// This code is contributed by vt_m


输出 :

31