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