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

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:
- Find the maximum character from index 1 to K, and find the dictionary mapping of the respective maximum character.
- If dictionary mapping is 1, increment all characters from 1 to K, i.e. ‘a’ becomes ‘b’, ‘b’ becomes ‘c’, . . . , ‘z’ becomes ‘a’.
- 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:
- 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).
- Now find the maximum element in that prefix and the number that it has been mapped to in Dict, say mp.
- Then apply the increment or decrement operation according to the number mp.
- Now, create a vector ans, to store the frequency of each element.
- Traverse the whole string and fill vector ans.
- Print the elements of vector ans.
Below is the implementation of the above approach:
C++
// C++ program for the above approach#include <bits/stdc++.h>using namespace std;// Function to find the frequency of// all character after performing N operationsvoid 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 codeint 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 approachimport java.util.*;class GFG{ // Function to find the frequency of // all character after performing 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 performing N operationsdef 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 codeif __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 approachusing System;public class GFG{ // Function to find the frequency of // all character after performing 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[26]; 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 |
Javascript
<script>// javascript program for the above approachlet m = { 'a': 0, 'b': 1}// Function to find the frequency of// all character after performing N operationsfunction processString(S, Dict){ let N = S.length; let mx = ''; // Vector to store the frequency // of all characters let ans = new Array(26).fill(0); for (let i = 0; i < N; i++) { mx = 96; for (let j = 0; j <= i; j++) { mx = Math.max(mx, S[j].charCodeAt(0)); } for (let 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].charCodeAt(0) + Dict[m[mx]] < 97) { S[j] = String.fromCharCode(S[j].charCodeAt(0) + Dict[m[mx]] + 26); } // If S[j] is 'z' and // Dict[S[j]] is 1 // then make S[j] equals to 'a' else if (S[j].charCodeAt(0) + Dict[m[mx]] > 122) { S[j] = String.fromCharCode(S[j].charCodeAt(0) + Dict[m[mx]] - 26); } else { S[j] = String.fromCharCode(S[j].charCodeAt(0) + Dict[m[mx]]); } } } for (let i = 0; i < N; i++) { ans[S[i].charCodeAt(0)]++; } ans.forEach(function(ele){ document.write(ele + " "); })}// Driver codelet S = ['a', 'b'];let 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);// The code is contributed by Gautam goel. </script> |
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)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



