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.
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 :
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Count pairs with set bits sum equal to K
- Count pairs from two arrays having sum equal to K
- Count of replacements required to make the sum of all Pairs of given type from the Array equal
- Count of index pairs with equal elements in an array | Set 2
- Maximum Count of pairs having equal Sum based on the given conditions
- Count pairs from an array having product of their sum and difference equal to 0
- Count pairs from an array having product of their sum and difference equal to 1
- Count pairs whose product modulo 10^9 + 7 is equal to 1
- Number of pairs such that path between pairs has the two vertices A and B
- Check if count of Alphabets and count of Numbers are equal in the given String
- Count ways to split a Binary String into three substrings having equal count of zeros
- 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
- Ways to form n/2 pairs such that difference of pairs is minimum
- Count pairs of non-overlapping palindromic sub-strings of the given string
- Count the pairs of vowels in the given string
- Count pairs of characters in a string whose ASCII value difference is K
- Count of pairs of strings whose concatenation forms a palindromic string
- Count number of triplets with product equal to given number with duplicates allowed | Set-2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.