# Count of each lowercase character after performing described operations for each prefix of length 1 to N

• Last Updated : 20 Jan, 2022

Given a string S containing N lowercase English alphabets and a dictionary Dict that maps all lowercase English alphabets from ‘a’ to ‘z’ to either 1 or -1. For a string of length K, the following operation can be applied:

1. Find the maximum character from index 1 to K, and find the dictionary mapping of the respective maximum character.
2. If dictionary mapping is 1, increment all characters from 1 to K, i.e. ‘a’ becomes ‘b’, ‘b’ becomes ‘c’, . . . , ‘z’ becomes ‘a’.
3. If dictionary mapping is -1, decrement all characters from 1 to K. i.e. ‘a’ becomes ‘z’, ‘b’ becomes ‘a’, . . . , ‘z’ becomes ‘y’

Perform the described operation for each prefix of the string of length 1 to N.

The task is to determine the count of each lowercase English alphabet after performing the described operation for each prefix of the string of length 1 to N.

Examples:

Input: S=”ab”, Dict[] = [1,-1, 1, 1, 1, 1, 1, 1, 1, 1, 1,-1, 1,-1, 1, 1, 1, 1,-1,-1,-1,1,1,-1,1-1]
Output: 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Explanation:
For Prefix of length 1: Max character = ‘a’, Mapping = 1. So, increment prefix string. S=”bb”.
For Prefix of length 2: Max character = ‘b’, Mapping = -1. So, decrement prefix string. S = “aa”.

Input: S=”abcb”, Dict[] = [1, -1, 1, 1, 1, -1, 1, 1, 1, -1, 1, -1, 1, -1, 1, 1, 1, 1,-1,-1,-1,1,1,-1,1-1]
Output: 0 0 3 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Explanation:
For Prefix of length 1: Max character = ‘a’, Mapping = 1. So, increment prefix string. S=”bbcb”.
For Prefix of length 2: Max character = ‘b’, Mapping = -1. So, decrement prefix string. S = “aacb”.
For Prefix of length 3: Max character = ‘c’, Mapping = 1. So, increment prefix string. S=”bbdb”.
For Prefix of length 4: Max character = ‘d’, Mapping = 1. So, increment prefix string. S = “ccec”.

Approach: The solution is based on greedy approach. Follow the below steps to solve this problem:

1. Run a loop from i = 0 to i < N and in that loop run another loop from j = i to j >= 0 (to traverse the prefix).
2. Now find the maximum element in that prefix and the number that it has been mapped to in Dict, say mp.
3. Then apply the increment or decrement operation according to the number mp.
4. Now, create a vector ans, to store the frequency of each element.
5. Traverse the whole string and fill vector ans.
6. Print the elements of vector ans.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `// Function to find the frequency of``// all character after performig N operations``void` `processString(string& S, vector<``int``>& Dict)``{``    ``int` `N = S.length();``    ``char` `mx;` `    ``// Vector to store the frequency``    ``// of all characters``    ``vector<``int``> ans(26, 0);``    ``for` `(``int` `i = 0; i < N; i++) {``        ``mx = 96;``        ``for` `(``int` `j = 0; j <= i; j++) {``            ``mx = max(mx, S[j]);``        ``}` `        ``for` `(``int` `j = 0; j <= i; j++) {``            ``// If S[j] is 'a' and``            ``// Dict[S[j]] is -1 then``            ``// make S[j] equals to 'z'``            ``if` `(S[j] + Dict[mx - ``'a'``] < 97) {``                ``S[j] = S[j] +``                    ``Dict[mx - ``'a'``] + 26;``            ``}` `            ``// If S[j] is 'z' and``            ``// Dict[S[j]] is 1``            ``// then make S[j] equals to 'a'``            ``else` `if` `(S[j] + Dict[mx - ``'a'``]``                     ``> 122) {``                ``S[j] = S[j] +``                    ``Dict[mx - ``'a'``] - 26;``            ``}` `            ``else` `{``                ``S[j] += Dict[mx - ``'a'``];``            ``}``        ``}``    ``}``    ``for` `(``int` `i = 0; i < N; i++) {``        ``ans[S[i] - ``'a'``]++;``    ``}` `    ``for` `(``auto` `x : ans) {``        ``cout << x << ``' '``;``    ``}``}` `// Driver code``int` `main()``{` `    ``string S = ``"ab"``;``    ``vector<``int``> Dict``        ``= { 1,  -1, 1, 1, 1, -1, 1,  1, ``           ``1, -1, 1,  -1,   1, -1, 1,  1, 1,``           ``1, -1, -1, -1, 1, 1,  -1, 1 - 1 };``    ``processString(S, Dict);``    ` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``import` `java.util.*;``class` `GFG``{` `  ``// Function to find the frequency of``  ``// all character after performig N operations``  ``static` `void` `processString(``char``[] S, ``int``[] Dict)``  ``{``    ``int` `N = S.length;``    ``char` `mx;` `    ``// Vector to store the frequency``    ``// of all characters``    ``int``[] ans = ``new` `int``[``26``];``    ``for` `(``int` `i = ``0``; i < N; i++) {``      ``mx = ``96``;``      ``for` `(``int` `j = ``0``; j <= i; j++) {``        ``mx = (``char``) Math.max(mx, S[j]);``      ``}` `      ``for` `(``int` `j = ``0``; j <= i; j++)``      ``{` `        ``// If S[j] is 'a' and``        ``// Dict[S[j]] is -1 then``        ``// make S[j] equals to 'z'``        ``if` `(S[j] + Dict[mx - ``'a'``] < ``97``) {``          ``S[j] = (``char``) (S[j] +``                         ``Dict[mx - ``'a'``] + ``26``);``        ``}` `        ``// If S[j] is 'z' and``        ``// Dict[S[j]] is 1``        ``// then make S[j] equals to 'a'``        ``else` `if` `(S[j] + Dict[mx - ``'a'``]``                 ``> ``122``) {``          ``S[j] = (``char``) (S[j] +``                         ``Dict[mx - ``'a'``] - ``26``);``        ``}` `        ``else` `{``          ``S[j] += Dict[mx - ``'a'``];``        ``}``      ``}``    ``}``    ``for` `(``int` `i = ``0``; i < N; i++) {``      ``ans[S[i] - ``'a'``]++;``    ``}` `    ``for` `(``int` `x : ans) {``      ``System.out.print(x +``" "``);``    ``}``  ``}` `  ``// Driver code``  ``public` `static` `void` `main(String[] args)``  ``{` `    ``char` `[]S = ``"ab"``.toCharArray();``    ``int``[] Dict``      ``= { ``1``,  -``1``, ``1``, ``1``, ``1``, -``1``, ``1``,  ``1``, ``         ``1``, -``1``, ``1``,  -``1``,   ``1``, -``1``, ``1``,  ``1``, ``1``,``         ``1``, -``1``, -``1``, -``1``, ``1``, ``1``,  -``1``, ``1` `- ``1` `};``    ``processString(S, Dict);``  ``}``}` `// This code is contributed by 29AjayKumar`

## Python3

 `# Python program for the above approach` `# Function to find the frequency of``# all character after performig N operations``def` `processString(S, ``Dict``):``    ``N ``=` `len``(S);``    ``mx ``=` `'';` `    ``# Vector to store the frequency``    ``# of all characters``    ``ans ``=` `[``0` `for` `i ``in` `range``(``26``)];``    ``for` `i ``in` `range``(N):``        ``mx ``=` `chr``(``96``);``        ``for` `j ``in` `range``(i``+``1``):``            ``mx ``=` `chr``(``max``(``ord``(mx), ``ord``(S[j])));` `        ``for` `j ``in` `range``(i ``+` `1``):` `            ``# If S[j] is ord('a') and``            ``# Dict[S[j]] is -1 then``            ``# make S[j] equals to 'z'``            ``if` `(``ord``(S[j]) ``+` `Dict``[``ord``(mx) ``-` `ord``(``'a'``)] < ``97``):``                ``S[j] ``=` `ord``(S[j] ``+` `Dict``[``ord``(mx) ``-` `ord``(``'a'``)] ``+` `26``);` `            ``# If S[j] is 'z' and``            ``# Dict[S[j]] is 1``            ``# then make S[j] equals to ord('a')``            ``elif` `(``ord``(S[j]) ``+` `Dict``[``ord``(mx) ``-` `ord``(``'a'``)] > ``122``):``                ``S[j] ``=` `ord``(``ord``(S[j]) ``+` `Dict``[``ord``(mx) ``-` `ord``(``'a'``)] ``-` `26``);`  `            ``else``:``                ``tempc ``=` `chr``(``ord``(S[j]) ``+` `Dict``[``ord``(mx) ``-` `ord``(``'a'``)]);``                ``S``=` `S[``0``:j]``+``tempc``+``S[j:]``                ``#S[j] = tempc;` `    ``for` `i ``in` `range``(N):``        ``ans[``ord``(S[i]) ``-` `ord``(``'a'``)] ``+``=` `1``;` `    ``for` `x ``in` `ans:``        ``print``(x, end``=``" "``);` `# Driver code``if` `__name__ ``=``=` `'__main__'``:``    ``S ``=` `"ab"``;``    ``Dict` `=` `[``1``, ``-``1``, ``1``, ``1``, ``1``, ``-``1``, ``1``, ``1``, ``1``,``            ``-``1``, ``1``, ``-``1``, ``1``, ``-``1``, ``1``, ``1``, ``1``,``            ``1``, ``-``1``, ``-``1``, ``-``1``, ``1``, ``1``, ``-``1``, ``1` `-` `1``];``    ``processString(S, ``Dict``);``    ` `    ``# This code is contributed by 29AjayKumar`

## C#

 `// C# program for the above approach``using` `System;` `public` `class` `GFG``{` `  ``// Function to find the frequency of``  ``// all character after performig N operations``  ``static` `void` `processString(``char``[] S, ``int``[] Dict)``  ``{``    ``int` `N = S.Length;``    ``char` `mx;` `    ``// List to store the frequency``    ``// of all characters``    ``int``[] ans = ``new` `int``;``    ``for` `(``int` `i = 0; i < N; i++) {``      ``mx = (``char``)96;``      ``for` `(``int` `j = 0; j <= i; j++) {``        ``mx = (``char``) Math.Max(mx, S[j]);``      ``}` `      ``for` `(``int` `j = 0; j <= i; j++)``      ``{` `        ``// If S[j] is 'a' and``        ``// Dict[S[j]] is -1 then``        ``// make S[j] equals to 'z'``        ``if` `(S[j] + Dict[mx - ``'a'``] < 97) {``          ``S[j] = (``char``) (S[j] +``                         ``Dict[mx - ``'a'``] + 26);``        ``}` `        ``// If S[j] is 'z' and``        ``// Dict[S[j]] is 1``        ``// then make S[j] equals to 'a'``        ``else` `if` `(S[j] + Dict[mx - ``'a'``]``                 ``> 122) {``          ``S[j] = (``char``) (S[j] +``                         ``Dict[mx - ``'a'``] - 26);``        ``}` `        ``else` `{``          ``S[j] += (``char``)Dict[mx - ``'a'``];``        ``}``      ``}``    ``}``    ``for` `(``int` `i = 0; i < N; i++) {``      ``ans[S[i] - ``'a'``]++;``    ``}` `    ``foreach` `(``int` `x ``in` `ans) {``      ``Console.Write(x +``" "``);``    ``}``  ``}` `  ``// Driver code``  ``public` `static` `void` `Main(String[] args)``  ``{` `    ``char` `[]S = ``"ab"``.ToCharArray();``    ``int``[] Dict``      ``= { 1,  -1, 1, 1, 1, -1, 1,  1, ``         ``1, -1, 1,  -1,   1, -1, 1,  1, 1,``         ``1, -1, -1, -1, 1, 1,  -1, 1 - 1 };``    ``processString(S, Dict);``  ``}``}` `// This code is contributed by 29AjayKumar`

Output
`2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 `

Time Complexity: O(N2)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up