Repeat last occurrence of each alphanumeric character to their position in character family times
Given a string str[] of size N, the task is to encode it in such a way that the last occurrence of each character occurs as long as its position in its family. As ‘a’ is the first character of its family (lower case alphabet), so it will remain ‘a’, but ‘b’ becomes ‘bb’, ‘D’ becomes ‘DDDD’ and so on. In case of numeric characters, the character occurs as many times as its value. As ‘1’ remains ‘1’, ‘2’ becomes ’22’, ‘0’ becomes ” and so on. But other than the last occurrence of a character, the rest must remain as they are. The characters other than (a-z), (A-Z), and (0-9), remain unaffected by such encoding.
Examples:
Input: str = “3bC”
Output: 333bbCCC
Explanation: In the given string, no character is repeated, hence all characters have one occurrence which is obviously their last. So every character will be encoded. As ‘3’ is a numeric character having the value 3 so it will occur thrice in the resultant string. ‘b’ is the second character of its family, so it will occur twice. And ‘C’ is the third capital character, hence it will occur three times.Input: str = “Ea2, 0, E”
Output: Ea22,, EEEEE
Explanation: ‘E’ at the beginning isn’t its last occurrence in the string, thus it will remain as it is in the resultant string. While ‘a’ and ‘2’ are their only and last occurrences in the string, they will be changed to ‘a’ and ’22’ respectively. The characters ‘, ‘ and ‘ ‘ will remain unaffected. And ‘0’ will also be changed to ”.
Approach: Traverse the string and keep track of the latest occurrence of each character using hashing and then encode for the last occurrence.
Follow the steps below to solve the problem:
- Initialize the variable string res as an empty string to store the result.
- Initialize the arrays small and capital of size 26 and num of size 10 with 0 to store the last occurrence of any character in the string str[].
- Iterate over the range [0, N] using the variable i and performing the following tasks:
- If str[i] is greater than equal to ‘0’ and less than equal to ‘9’, then set the value of num[str[i] – ‘0’] as i.
- Else If str[i] is greater than equal to ‘a’ and less than equal to ‘z’, then set the value of small[str[i] – ‘a’] as i.
- Else If str[i] is greater than equal to ‘A’ and less than equal to ‘Z’, then set the value of capital[str[i] – ‘A’] as i.
- Iterate over the range [0, N] using the variable i and performing the following tasks:
- If str[i] is greater than equal to ‘0’ and less than equal to ‘9’ and num[str[i]-‘0’] is equal to i, then initialize the variable occur as str[i]-‘0’ and append str[i] in the resultant string res, occur number of times.
- Else If str[i] is greater than equal to ‘a’ and less than equal to ‘z’ and small[str[i]-‘a’] is equal to i, then initialize the variable occur as str[i]-‘a’ and append str[i] in the resultant string res, occur number of times.
- Else If str[i] is greater than equal to ‘A’ and less than equal to ‘Z’ and capital[str[i]-‘0’] is equal to i, then initialize the variable occur as str[i]-‘A’ and append str[i] in the resultant string res, occur number of times.
- Else, append str[i] in the resultant string res.
- After performing the above steps, print the string res as the answer.
Below is the implementation of the above approach.
C++
// C++ program for the above approach#include <bits/stdc++.h>using namespace std; // Function to encode the given stringvoid encodeString(string str){ // Variable string to store the result string res = ""; // Arrays to store the last occuring index // of every character in the string int small[26] = { 0 }, capital[26] = { 0 }, num[10] = { 0 }; // Length of the string int n = str.size(); // Iterate over the range for (int i = 0; i < n; i++) { // If str[i] is between 0 and 9 if (str[i] >= '0' && str[i] <= '9') { num[str[i] - 48] = i; } // If str[i] is between a and z else if (str[i] >= 'a' && str[i] <= 'z') { small[str[i] - 97] = i; } // If str[i] is between A and Z else if (str[i] >= 'A' && str[i] <= 'Z') { capital[str[i] - 65] = i; } } // Iterate over the range for (int i = 0; i < n; i++) { // If str[i] is between a and z and i // is the last occurence in str if ((str[i] >= 'a' && str[i] <= 'z') && small[str[i] - 97] == i) { int occ = str[i] - 96; while (occ--) { res += str[i]; } } // If str[i] is between A and Z and i // is the last occurence in str else if ((str[i] >= 'A' && str[i] <= 'Z') && capital[str[i] - 65] == i) { int occ = str[i] - 64; while (occ--) { res += str[i]; } } // If str[i] is between 0 and 9 and i // is the last occurence in str else if ((str[i] >= '0' && str[i] <= '9') && num[str[i] - 48] == i) { int occ = str[i] - 48; while (occ--) { res += str[i]; } } else { res += str[i]; } } // Print the result cout << res;} // Driver Codeint main(){ string str = "Ea2, 0, E"; encodeString(str); return 0;} |
Ea22, , EEEEE
Time Complexity: O(N)
Auxiliary Space: O(M) (Size of the resultant string)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.


