Home  >  Article  >  Backend Development  >  Count of each lowercase character in each prefix of length 1 to N after performing the described operation

Count of each lowercase character in each prefix of length 1 to N after performing the described operation

WBOY
WBOYforward
2023-09-15 09:05:02592browse

Count of each lowercase character in each prefix of length 1 to N after performing the described operation

In this problem, we need to perform the given operation for each string prefix. Finally, we need to count the frequency of each character.

We can use a greedy algorithm to solve this problem. We need to take each prefix of length K and update its characters according to the given conditions. We can use map to calculate the frequency of characters in the final string.

Problem Statement - We are given a string tr containing N lowercase alphabetic characters. Additionally, we are given a mapping list, containing a total of 26 elements. Each element is mapped to lowercase characters based on its value. For example, mapping[0] maps to "a", mapping[1] maps to "b", and mapping[25] maps to "z". Additionally, the mapped array contains 1 or -1.

We need to do the following.

  • Get the maximum characters from the prefix of length K and get the mapping value from the 'mapping' array.

  • If the mapped value is 1, increase all prefix elements by 1.

  • If the mapped value is -1, decrement all prefix elements by 1.

Here, adding elements means ‘a’ −> ‘b’, ‘b’ −> ‘c’,… ‘z’ −> ‘a’.

Decreasing elements mean, ‘a’->‘z’, ‘b’->‘a’,…. ‘z’->‘y’.

We need to perform the above operation for each prefix of length 1

ExampleExample

enter

mapping = {-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}, S = ‘progress’

Output

0 0 0 1 0 1 0 0 0 0 0 0 1 0 0 2 2 1 0 0 0 0 0 0 0 0

illustrate

  • In a prefix of length 1, the largest character is 'p', which maps to -1. Therefore, the updated string will be 'orogress'.

  • In a prefix of length 2, the maximum character is ‘r’ and the mapping is -1. Therefore, the updated string will be "nqogress".

  • In a prefix of length 3, the largest character is 'q', and the mapping value is 1. Therefore, the updated string is 'orpgress'.

  • When we are done with everything, the final string will be 'pqmfpdqr', which contains 1 'f', 2 'p', 2 'q', 1 'm', 1 'd' and 1 'd' 'r'. In the output, we print the frequency of each character in the resulting string.

enter

mapping = {-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}, S = "ab", 

Output

1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Explanation− After performing all operations, the final string is 'ac' and we printed the frequency of each character.

method one

In this method, we will iterate through the string and take the value of K equal to the index P. After that, we will take the prefix with length equal to P, find the largest character, take the mapped value, and update all prefix characters accordingly.

algorithm

Step 1 − Define the ‘max_char’ variable to store the maximum character for the given prefix.

Step 2 − Similarly, initialize a list of length 26 with zeros in order to store the frequency of each character in the final string.

Step 3- Start looping through the string and initialize the "max_char" variable with 96 inside the loop.

Step 4 - Use nested loops to find the largest character from a prefix of length p.

Step 5 - Update each character of the prefix by adding the mapped value of max_char.

Step 7 - If the updated character is smaller than "a", update it to "z".

Step 8 - If the updated character is larger than "z", update it to "a".

Step 9− Finally, store the frequency of each character in a list by looping through the updated string.

Step 10- Print the frequency of characters.

Example

#include <bits/stdc++.h>
using namespace std;

void performOperations(string &str, vector<int> &mapping) {
    int len = str.length();
    char max_char;
    //  array to store the final frequency of each character
    int freq[26] = {0};
    for (int p = 0; p < len; p++) {
        max_char = 96;
        // Get the maximum character from the prefix string
        for (int q = 0; q <= p; q++) {
            max_char = max(max_char, str[q]);
        }
        // Update the prefix string by adding the max character's value.
        for (int q = 0; q <= p; q++) {
            // adding the mapping value to the current character
            str[q] += mapping[max_char - 'a'];
            // If the updated value is greater than z or less than a, update it
            if (str[q] < 'a') {
                str[q] = 'z';
            } else if (str[q] > 'z') {
                str[q] = 'a';
            }
        }
    }
    // Counting frequency of each character
    for (int p = 0; p < len; p++) {
        freq[str[p] - 'a']++;
    }
    // print count of each character in the updated string
    for (auto ch : freq) {
        cout << ch << ' ';
    }
}
int main() {
    string S = "progress";
    vector<int> mapping = {-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};
    performOperations(S, mapping);
    return 0;
}

Output

0 0 0 1 0 1 0 0 0 0 0 0 1 0 0 2 2 1 0 0 0 0 0 0 0 0

Time complexity− O(N*N) because we use two nested loops to traverse the string.

Space complexity− O(1), because we use constant space to store the frequency of characters.

in conclusion

We perform the given operation on the input string and print the character frequency of the updated string in the output. Programmers can also use maps in C to store character frequencies instead of using lists. For more practice, the programmer could try printing the cumulative frequency of each character in the updated string.

The above is the detailed content of Count of each lowercase character in each prefix of length 1 to N after performing the described operation. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete