Find frequency of each character with positions in given Array of Strings
Given an array, arr[] consisting of N strings where each character of the string is lower case English alphabet, the task is to store and print the occurrence of every distinct character in every string.
Examples:
Input: arr[] = { “geeksforgeeks”, “gfg” }
Output: Occurences of: e = [1 2] [1 3] [1 10] [1 11]
Occurences of: f = [1 6] [2 2]
Occurences of: g = [1 1] [1 9] [2 1] [2 3]
Occurences of: k = [1 4] [1 12]
Occurences of: o = [1 7]
Occurences of: r = [1 8]
Occurences of: s = [1 5] [1 13]Input: arr[] = { “abc”, “ab” }
Output: Occurences of: a = [1 1] [2 1]
Occurences of: b = [1 2] [2 2]
Occurences of: c = [1 3]
Approach: The above problem can be solved using Map and Vector data structures. Follow the steps below to solve the problem:
- Initialize a map<char, vector<pair<int, int>> > say mp to store the occurrences of a character in the vector of pairs, where each pair stores the index of the string in array as the first element and the position of the character in the string as the second element.
- Traverse the vector arr using a variable i and perform the following step:
- Iterate over the characters of the string arr[i] using variable j and in each iteration push the pair {i+1, j+1} in the vector mp[arr[i][j]].
- Finally, after completing the above steps, print the occurrences of every character by iterating over the map mp.
Below is the implementation of the above approach:
C++
// C++ program for the above approach#include <bits/stdc++.h>using namespace std;// Function to print every occurence// of every characters in every stringvoid printOccurences(vector<string> arr, int N){ map<char, vector<pair<int, int> > > mp; // Iterate over the vector arr[] for (int i = 0; i < N; i++) { // Traverse the string arr[i] for (int j = 0; j < arr[i].length(); j++) { // Push the pair of{i+1, j+1} // in mp[arr[i][j]] mp[arr[i][j]].push_back( make_pair(i + 1, j + 1)); } } // Print the occurences of every // character 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; }}// Driver Codeint main(){ // Input vector<string> arr = { "geeksforgeeks", "gfg" }; int N = arr.size(); // Function call printOccurences(arr, N);} |
Python3
# Python3 program for the above approach# Function to print every occurence# of every characters in every stringdef printOccurences(arr, N): mp = [[] for i in range(26)] # Iterate over the vector arr[] for i in range(N): # Traverse the string arr[i] for j in range(len(arr[i])): # Push the pair of{i+1, j+1} # in mp[arr[i][j]] mp[ord(arr[i][j]) - ord('a')].append( (i + 1, j + 1)) # print(mp) # Print the occurences of every # character for i in range(26): if len(mp[i]) == 0: continue print("Occurences of:", chr(i + ord('a')), "=", end = " ") for j in mp[i]: print("[" + str(j[0]) + " " + str(j[1]) + "] ", end = "") print()# Driver Codeif __name__ == '__main__': # Input arr= [ "geeksforgeeks", "gfg" ] N = len(arr) # Function call printOccurences(arr, N)# This code is contributed by mohit kumar 29 |
Javascript
<script>// JavaScript program for the above approach// Function to print every occurence// of every characters in every stringfunction printOccurences(arr, N) { let mp = new Map(); // Iterate over the vector arr[] for (let i = 0; i < N; i++) { // Traverse the string arr[i] for (let j = 0; j < arr[i].length; j++) { // Push the pair of{i+1, j+1} // in mp[arr[i][j]] if (mp.has(arr[i][j])) { let temp = mp.get(arr[i][j]); temp.push([i + 1, j + 1]); mp.set(arr[i][j], temp); } else { mp.set(arr[i][j], [[i + 1, j + 1]]); } } } // Print the occurences of every // character for (let it of new Map([...mp.entries()].sort())) { document.write("Occurences of: " + it[0] + " = "); for (let j = 0; j < it[1].length; j++) { document.write(" [" + it[1][j][0] + " " + it[1][j][1] + "] "); } document.write("<br>"); }}// Driver Code// Inputlet arr = ["geeksforgeeks", "gfg"];let N = arr.length;// Function callprintOccurences(arr, N);</script> |
Occurences of: e = [1 2] [1 3] [1 10] [1 11] Occurences of: f = [1 6] [2 2] Occurences of: g = [1 1] [1 9] [2 1] [2 3] Occurences of: k = [1 4] [1 12] Occurences of: o = [1 7] Occurences of: r = [1 8] Occurences of: s = [1 5] [1 13]
Time Complexity: O(N*M), where M is the length of the longest string.
Auxiliary Space: O(N*M)
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. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.


