# Count number of equal pairs in a string

Given a string s, find the number of pairs of characters that are same. Pairs (s[i], s[j]), (s[j], s[i]), (s[i], s[i]), (s[j], s[j]) should be considered different.

**Examples :**

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

The **naive approach** will be you to run two nested for loops and find out all pairs and keep a count of all pairs. But this is not efficient enough for longer length of strings.

For an **efficient approach**, we need to count the number of equal pairs in **linear time**. Since pairs (x, y) and pairs (y, x) are considered different. We need to use a hash table to store the count of all occurrences of a character.So we know if a character occurs twice, then it will have 4 pairs – **(i, i), (j, j), (i, j), (j, i)**. So using a hash function, store the occurrence of each character, then for each character the number of pairs will be occurrence^2. Hash table will be 256 in length as we have 256 characters.

Below is the implementation of the above approach :

## C++

`// CPP program to count the number of pairs ` `#include <bits/stdc++.h> ` `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; ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

**Output :**

31

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- Count pairs with set bits sum equal to K
- Count pairs from two arrays having sum equal to K
- Check if count of Alphabets and count of Numbers are equal in the given String
- Count the pairs of vowels in the given string
- Count pairs of non-overlapping palindromic sub-strings of the given string
- Count pairs of characters in a string whose ASCII value difference is K
- Total character pairs from two strings, with equal number of set bits in their ascii value
- String Range Queries to find the number of subsets equal to a given String
- Count number of pairs (i, j) such that arr[i] * arr[j] = arr[i] + arr[j]
- Count pairs with Bitwise AND as ODD number
- Count number of triplets with product equal to given number with duplicates allowed | Set-2
- Count number of triplets with product equal to given number with duplicates allowed
- Count number of ordered pairs with Even and Odd Sums
- Count ways to partition a string such that both parts have equal distinct characters
- Count Substrings with equal number of 0s, 1s and 2s
- Count the number of subsequences of length k having equal LCM and HCF
- Number of ways to insert two pairs of parentheses into a string of N characters
- Count the number of strings in an array whose distinct characters are less than equal to M
- Split the binary string into substrings with equal number of 0s and 1s
- Check length of a string is equal to the number appended at its last

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.