- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count of each Lowercase Character after Performing described Operations for each Prefix of Length 1 to N
In this problem, we need to perform the given operations with each string prefix. In the end, we need to count the frequency of each character.
We can follow the greedy approach to solve the problem. We need to take each prefix of length K and update its characters according to the given conditions. We can use the map to count the frequency of characters in the final string.
Problem statement − We have given the strings tr containing the N lowercase alphabetical characters. Also, we have given the mapping list, which contains total 26 elements. Each element is mapped to the lowercase character according to its value. For example, mapping[0] is mapped to ‘a’, mapping[1] is mapped to ‘b’, and mapping[25] is mapped to ‘z’. Furthermore, the mapping array contains either 1 or −1.
We need to perform the below operations.
Get the maximum character from the prefix of length K, and get the mapped value from the ‘mapping’ array.
If the mapped value is 1, increase all prefix elements by 1.
If the mapped value is −1, decrease all prefix elements by 1.
Here, increasing elements means ‘a’ −> ‘b’, ‘b’ −> ‘c’, … ‘z’ −> ‘a’.
The decreasing elements means, ‘a’ −> ‘z’, ‘b’ −> ‘a’, …. ‘z’ −> ‘y’.
We need to perform the above operation for each prefix of length 1 <= K <= N. We need to print the frequency of each character after performing the above operations.
Sample examples
Input
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
Explanation
In the prefix of length 1, the max character is ‘p’, and the mapping is −1. So, the updated string will be ‘orogress’.
In the prefix of length 2, the maximum character is ‘r’, and the mapping is −1. So, an updated string will be ‘nqogress’.
In the prefix of length 3, the maximum character is ‘q’, and the mapping value is 1. So, the updated string is ‘orpgress’.
When we complete all operations, the final string will be ‘pqmfpdqr’, which contains 1 ‘f’, 2 ‘p’, 2 ‘q’, 1 ‘m’, 1 ‘d’, and 1 ‘r’. In the output, we printed the frequency of each character in the resultant string.
Input
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.
Approach 1
In this approach, we will traverse the string and take the value of the K as equal to the index P. After that, we will take the prefix of length equal to P, find the maximum character, take the mapped value, and update all prefix characters accordingly.
Algorithm
Step 1 − Define the ‘max_char’ variable to store the maximum character of the given prefix.
Step 2 − Also, initialize the list of length 26 with zero to store the frequency of each character in the final string.
Step 3 − Start traversing the string, and initialize the ‘max_char’ variable with 96 inside the loop.
Step 4 − Use a nested loop to find the maximum character from the prefix of length p.
Step 5 − Update each character of the prefix by adding the mapped value of max_char.
Step 7 − If an updated character is less than ‘a’, update it to ‘z’.
Step 8 − If an updated character is greater than ‘z’, update it to ‘a’.
Step 9 − In the last, store the frequency of each character in the list by traversing the updated string.
Step 10 − Print the characters' frequency.
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) as we traverse the string using two nested loops.
Space complexity − O(1) as we use constant space to store the frequency of characters.
Conclusion
We performed the given operations in the input string and printed the updated string’s character’s frequency in the output. Programmers can also use the map in C++ to store the character frequency rather than using the list. For more practice, programmers can try to print the cumulative frequency of each character in the updated string.
To Continue Learning Please Login
Login with Google