Count of pairs of strings which differ in exactly one position | CPP implementation of the approach ; Function to return the count of same pairs ; Function to return total number of strings which satisfy required condition ; Dictionary changed will store strings with wild cards Dictionary same will store strings that are equal ; Iterating for all strings in the given array ; If we found the string then increment by 1 Else it will get default value 0 ; Iterating on a single string ; Incrementing the string if found Else it will get default value 0 ; Return counted pairs - equal pairs ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int pairCount ( map < string , int > & d ) { int sum = 0 ; for ( auto i : d ) sum += ( i . second * ( i . second - 1 ) ) / 2 ; return sum ; } int difference ( vector < string > & array , int m ) { map < string , int > changed , same ; for ( auto s : array ) { same [ s ] ++ ; for ( int i = 0 ; i < m ; i ++ ) { changed [ t ] ++ ; } } return pairCount ( changed ) - pairCount ( same ) * m ; } int main ( ) { int n = 3 , m = 3 ; vector < string > array = { " abc " , " abd " , " bbd " } ; cout << difference ( array , m ) << endl ; return 0 ; }