Count of each lowercase character after performing described operations for each prefix of length 1 to N
Last Updated :
14 Feb, 2023
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++
#include <bits/stdc++.h>
using namespace std;
void processString(string& S, vector<int>& Dict)
{
int N = S.length();
char mx;
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] + Dict[mx - 'a'] < 97) {
S[j] = S[j] +
Dict[mx - 'a'] + 26;
}
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 << ' ';
}
}
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
import java.util.*;
class GFG
{
static void processString(char[] S, int[] Dict)
{
int N = S.length;
char mx;
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] + Dict[mx - 'a'] < 97) {
S[j] = (char) (S[j] +
Dict[mx - 'a'] + 26);
}
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 +" ");
}
}
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);
}
}
Python3
def processString(S, Dict):
N = len(S);
mx = '';
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 (ord(S[j]) + Dict[ord(mx) - ord('a')] < 97):
S[j] = ord(S[j] + Dict[ord(mx) - ord('a')] + 26);
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:]
for i in range(N):
ans[ord(S[i]) - ord('a')] += 1;
for x in ans:
print(x, end=" ");
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);
C#
using System;
public class GFG
{
static void processString(char[] S, int[] Dict)
{
int N = S.Length;
char mx;
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] + Dict[mx - 'a'] < 97) {
S[j] = (char) (S[j] +
Dict[mx - 'a'] + 26);
}
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 +" ");
}
}
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);
}
}
Javascript
<script>
let m = {
'a': 0,
'b': 1
}
function processString(S, Dict)
{
let N = S.length;
let mx = '';
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].charCodeAt(0) + Dict[m[mx]] < 97) {
S[j] = String.fromCharCode(S[j].charCodeAt(0) + Dict[m[mx]] + 26);
}
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 + " ");
})
}
let 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);
</script>
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)
Like Article
Suggest improvement
Share your thoughts in the comments
Add Your Comment
Please Login to comment...