Find frequency of each character with positions in given Array of Strings Last Updated : 01 Feb, 2023 Comments Improve Suggest changes Like Article Like Report 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: Occurrences of: e = [1 2] [1 3] [1 10] [1 11] Occurrences of: f = [1 6] [2 2] Occurrences of: g = [1 1] [1 9] [2 1] [2 3] Occurrences of: k = [1 4] [1 12] Occurrences of: o = [1 7] Occurrences of: r = [1 8] Occurrences of: s = [1 5] [1 13] Input: arr[] = { "abc", "ab" }Output: Occurrences of: a = [1 1] [2 1] Occurrences of: b = [1 2] [2 2] Occurrences 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 occurrence // of every characters in every string void printOccurrences(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 occurrences of every // character for (auto it : mp) { cout << "Occurrences of: " << it.first << " = "; for (int j = 0; j < (it.second).size(); j++) { cout << "[" << (it.second)[j].first << " " << (it.second)[j].second << "] "; } cout << endl; } } // Driver Code int main() { // Input vector<string> arr = { "geeksforgeeks", "gfg" }; int N = arr.size(); // Function call printOccurrences(arr, N); } Java import java.util.*; import java.util.Map.Entry; class GFG { public static void printOccurrences(List<String> arr, int N) { Map<Character, List<Pair<Integer, Integer>>> mp = new HashMap<>(); // Iterate over the List arr[] for (int i = 0; i < N; i++) { // Traverse the string arr[i] for (int j = 0; j < arr.get(i).length(); j++) { // Push the pair of{i+1, j+1} // in mp[arr[i][j]] if (!mp.containsKey(arr.get(i).charAt(j))) { mp.put(arr.get(i).charAt(j), new ArrayList<>()); } mp.get(arr.get(i).charAt(j)).add(new Pair<>(i + 1, j + 1)); } } // Print the occurrences of every // character for (Entry<Character, List<Pair<Integer, Integer>>> it : mp.entrySet()) { System.out.print("Occurrences of: " + it.getKey() + " = "); for (int j = 0; j < it.getValue().size(); j++) { System.out.print("[" + it.getValue().get(j).getKey() + " " + it.getValue().get(j).getValue() + "] "); } System.out.println(); } } // Driver Code public static void main(String[] args) { // Input List<String> arr = Arrays.asList("geeksforgeeks", "gfg"); int N = arr.size(); // Function call printOccurrences(arr, N); } } // custom pair class class Pair<T, U> { private T key; private U value; public Pair(T key, U value) { this.key = key; this.value = value; } public T getKey() { return key; } public U getValue() { return value; } } // This code is contributed by aadityapburujwale Python3 # Python3 program for the above approach # Function to print every occurrence # of every characters in every string def printOccurrences(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 occurrences of every # character for i in range(26): if len(mp[i]) == 0: continue print("Occurrences of:", chr(i + ord('a')), "=", end = " ") for j in mp[i]: print("[" + str(j[0]) + " " + str(j[1]) + "] ", end = "") print() # Driver Code if __name__ == '__main__': # Input arr= [ "geeksforgeeks", "gfg" ] N = len(arr) # Function call printOccurrences(arr, N) # This code is contributed by mohit kumar 29 C# using System; using System.Collections.Generic; class Program { static void Main(string[] args) { string[] arr = { "geeksforgeeks", "gfg" }; int N = arr.Length; Dictionary<char, List<Tuple<int, int>>> mp = new Dictionary<char, List<Tuple<int, int>>>(); // Push the pair of{i+1, j+1} // in mp[arr[i][j]] for (int i = 0; i < N; i++) { for (int j = 0; j < arr[i].Length; j++) { if (!mp.ContainsKey(arr[i][j])) { mp[arr[i][j]] = new List<Tuple<int, int>>(); } mp[arr[i][j]].Add(Tuple.Create(i + 1, j + 1)); } } // Print the occurrences of every // character foreach (var item in mp) { Console.WriteLine("Occurrences of: " + item.Key + " = "); foreach (var pair in item.Value) { Console.Write("[" + pair.Item1 + " " + pair.Item2 + "] "); } Console.WriteLine(); } } } // This code is contributed by divyansh2212 JavaScript <script> // JavaScript program for the above approach // Function to print every occurrence // of every characters in every string function printOccurrences(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 occurrences of every // character for (let it of new Map([...mp.entries()].sort())) { document.write("Occurrences 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 // Input let arr = ["geeksforgeeks", "gfg"]; let N = arr.length; // Function call printOccurrences(arr, N); </script> OutputOccurrences of: e = [1 2] [1 3] [1 10] [1 11] Occurrences of: f = [1 6] [2 2] Occurrences of: g = [1 1] [1 9] [2 1] [2 3] Occurrences of: k = [1 4] [1 12] Occurrences of: o = [1 7] Occurrences of: r = [1 8] Occurrences 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) Comment More infoAdvertise with us Next Article Find frequency of each character with positions in given Array of Strings V vermaaayush68 Follow Improve Article Tags : Strings Searching Data Structures DSA Arrays cpp-map frequency-counting +3 More Practice Tags : ArraysData StructuresSearchingStrings Similar Reads Find frequency of all characters across all substrings of given string Given a string S containing all lowercase characters and its length N. Find frequency of all characters across all substrings of the given string. Examples: Input: N = 3, S = "aba"Output: a 6b 4Explanation: The substrings are: a, b, a, ab, ba, aba. The frequency of each character: a = 6, b = 4. Henc 4 min read Find the Suffix Array of given String with no repeating character Given a string str of size N, the task is to find the suffix array of the given string. Note: A suffix array is a sorted array of all suffixes of a given string. Examples: Input: str = "prince"Output: 4 5 2 3 0 1Explanation: The suffixes are0 prince 4 ce1 rince Sort the suffixes 5 e 2 ince --------- 6 min read Count of strings that does not contain any character of a given string Given an array arr containing N strings and a string str, the task is to find the number of strings that do not contain any character of string str. Examples: Input: arr[] = {"abcd", "hijk", "xyz", "ayt"}, str="apple"Output: 2Explanation: "hijk" and "xyz" are the strings that do not contain any char 8 min read Find Array formed by reversing Prefix each time given character is found Given an array arr[] of length N consisting of uppercase English letters only and a letter ch. the task is to find the final array that will form by reversing the prefix each time the letter ch is found in the array. Examples: Input: arr[] = {'A', 'B', 'X', 'C', 'D', 'X', 'F'}, ch= 'X'Output: D C X 6 min read Total length of string from given Array of strings composed using given characters Given a list of characters and an array of strings, find the total length of all strings in the array of strings that can be composed using the given characters.Examples: Input: string = ["mouse", "me", "bat", "lion"], chars = "eusamotb" Output: 10 Explanation: The strings that can be formed using t 6 min read Count of strings with frequency of each character at most X and length at least Y Given an array arr[] of strings and integers X and Y, the task is to find the count of strings with frequency of each character at most X and length of the string at least Y. Examples: Input: arr[] = { "ab", "derdee", "erre" }, X = 2, Y = 4Output: 1Explanation: Strings with character frequency at mo 6 min read Count of substrings formed using a given set of characters only Given a string str and an array arr[] of K characters, the task is to find the number of substrings of str that contain characters only from the given character array arr[]. Note: The string str and the arr[] contain only lowercase alphabets. Examples: Input: S = "abcb", K = 2, charArray[] = {'a', ' 8 min read Count of number of given string in 2D character array Given a 2-dimensional character array and a string, we need to find the given string in a 2-dimensional character array, such that individual characters can be present left to right, right to left, top to down or down to top. Examples: Input : a ={ {D,D,D,G,D,D}, {B,B,D,E,B,S}, {B,S,K,E,B,K}, {D,D,D 9 min read Find the number of occurrences of a character upto preceding position Given a string S of length N and an integer P(1?P?N) denoting the position of a character in the string. The task is to find the number of occurrences of the character present at position P up to P-1 index. Examples: Input : S = "ababababab", P = 9 Output : 4 Character at P is 'a'. Number of occurre 7 min read Queries to find first occurrence of a character in a given range Given a string S of length N and an array Q[][] of dimension M Ã 3 consisting of queries of type {L, R, C}, the task is to print the first index of the character C in the range [L, R], if found. Otherwise, print -1. Examples: Input: S= "abcabcabc", Q[][] = { { 0, 3, 'a' }, { 0, 2, 'b' }, { 2, 4, 'z' 9 min read Like