Find frequency of each character with positions in given Array of Strings | C ++ program for the above approach ; Function to print every occurence of every characters in every string ; Iterate over the vector arr [ ] ; Traverse the string arr [ i ] ; Push the pair of { i + 1 , j + 1 } in mp [ arr [ i ] [ j ] ] ; Print the occurences of every character ; Driver Code ; Input ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printOccurences ( vector < string > arr , int N ) { map < char , vector < pair < int , int > > > mp ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < arr [ i ] . length ( ) ; j ++ ) { mp [ arr [ i ] [ j ] ] . push_back ( make_pair ( i + 1 , j + 1 ) ) ; } } for ( auto it : mp ) { cout << " Occurences ▁ of : ▁ " << it . first << " ▁ = ▁ " ; for ( int j = 0 ; j < ( it . second ) . size ( ) ; j ++ ) { cout << " [ " << ( it . second ) [ j ] . first << " ▁ " << ( it . second ) [ j ] . second << " ] ▁ " ; } cout << endl ; } } int main ( ) { vector < string > arr = { " geeksforgeeks " , " gfg " } ; int N = arr . size ( ) ; printOccurences ( arr , N ) ; }