Find the final String by incrementing prefixes of given length


In this problem, we need to increase each character of multiple prefixes of size given in the array. The naïve approach to solving the problem is to take each prefix of the size given in the array and increment each character of the prefix by 1.

The best approach is to create the prefix array using the given array and update each string character in the single iteration.

Problem statement − We have given a string alpha of length N. Also, we have given a prefixes array containing the positive integer. The prefixes[i] represent that take prefix of length prefixes[i] from the string and increment each character of the prefixes by 1. It’s also given that increment character cyclically. So, ‘z’ becomes ‘a’.

Sample Examples

Input 

alpha = "abcd";  prefixes[] = { 1, 1, 3 };

Output 

‘dcdd’

Explanation

  • As arr[0] is 1, take the prefix of length 1, and increment each prefix character. The resultant string will be ‘bbcd’.

  • For the second operation, take the prefix of length 1, and increment each character. The resultant string will be ‘cbcd’.

  • In the last operation, we need to take the prefix of length 3, and the resultant string will be ‘dcdd’.

Input 

alpha = "aabc"; prefixes[] = { 1, 2, 3, 4, 4, 3 };

Output 

‘gffe’

Explanation

After incrementing each character of each prefix of a given size, the resultant string is ‘gffe’.

Input 

alpha = "xyz"; prefixes[] = { 3, 2, 1 };

Output 

‘aaa’

Explanation

  • After incrementing the first 3 characters, the string becomes ‘yza’.

  • After incrementing the first 2 characters, the string becomes ‘zaa’.

  • After incrementing the first 1 character, the string becomes ‘aaa’.

Approach 1

In this approach, we will traverse through the array and take the string prefix of length prefixes[i]. After that, we will traverse the prefix string and increase each character by 1.

Algorithm

  • Step 1 − Traverse the array prefixes using the for loop.

  • Step 2 − Traverse the string from index 0 to prefixes[p].

  • Step 3 − If the character at the index in the string is ‘z’, update the character to ‘a’. Otherwise, add 1 to the character.

  • Step 4 − Return the alpha string when all the loop iterations are completed.

Example

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

string getFinalString(string alpha, int pref_array[], int pref_len) {
   // Traverse prefix array
   for (int p = 0; p < pref_len; p++) {
   
      // Incrementing the pref_array[p] elements cyclically
      for (int q = 0; q < pref_array[p]; q++) {
      
         // If the current character is 'z'
         if (alpha[q] == 'z') {
            alpha[q] = 'a';
         } else {
         
         // Increment character by 1
            alpha[q] += 1;
         }
      }
   }
   return alpha;
}
int main() {
   string alpha = "abcd";
   int prefixes[] = { 1, 1, 3 };
   int pref_len = sizeof(prefixes) / sizeof(prefixes[0]);
   cout << "The final string is " << getFinalString(alpha, prefixes, pref_len) << endl;
   return 0;
}

Output

The final string is dcdd

Time complexity – O(N*M), where N is a string length, and M is an array length.

Space complexity – O(1), as we update the alpha string.

Approach 2

In this approach, we will create a prefix array and increment all characters of the string in a single iteration.

Algorithm

  • Step 1 − Define an array named ‘arr’ of length equal to prefixes array length + 1, and initialize all elements with 0.

  • Step 2 − Start traversing the prefixes array and decrement arr[pref_array[p]] by 1. Also, increment arr[0] in each iteration.

  • Step 3 − Now, traverse the ‘arr’ array from the 1st index, and add the previous element to the current array element to prepare the prefix array.

  • Step 4 − Next, start traversing the string and increment each character’s value according to the ‘arr’ element’s value.

  • Step 5 − Take the modulo of array element with 26 in the loop.

  • Step 6 − If arr[p] + alpha[p] is greater than ‘z’, add arr[p] – 26 to alpha[p]. Otherwise, add only arr[p] to alpha[p].

  • Step 7 − Return the updated alpha string.

Example

Let’s debug the example below with sample input to know how it works.

  • Initially, the array arr is [0, 0, 0, 0, 0, 0, 0].

  • The array ‘arr’ has 0-based indexing. The prefixes array contains elements between 1 to string length. So, we can say that we need to increment string characters till prefixes[p] – 1 index while performing prexies[p] operation. In each operation, we always need to increment the first element as prexies contain elements in the range of 1 to string length. So, we increment arr[0] in each iteration.

  • After the iteration of the prexies array, the arr will be [6, -1, -1, -2, -2, 0, 0].

  • In the next step, we add the previous element to the current array element. So, arr will be [6, 5, 4, 2, 2, 2, 2].

  • Next, take the first 4 elements and increment string characters cyclically.

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

string getFinalString(string alpha, int pref_array[], int pref_len) {
   // Array initialized with 0
   int arr[pref_len + 1] = { 0 };
   
   // Update the array according to the prefix array value
   for (int p = 0; p < pref_len; p++) {
      arr[pref_array[p]] -= 1;
      arr[0] += 1;
   }
   
   // Create prefix array
   for (int p = 1; p < pref_len; p++) {
      arr[p] += arr[p - 1];
   }
   
   // Change character cyclically
   for (int p = 0; p < pref_len; p++) {
      arr[p] = (arr[p]) % 26;
      if (arr[p] + alpha[p] > 'z')
      alpha[p] = (alpha[p] + arr[p] - 26);
      else
      alpha[p] = alpha[p] + arr[p];
   }
   return alpha;
}
int main() {
   string alpha = "aabc";
   int prefixes[] = { 1, 2, 3, 4, 4, 3 };
   int pref_len = sizeof(prefixes) / sizeof(prefixes[0]);
   cout << "The final string is " << getFinalString(alpha, prefixes, pref_len) << endl;
   return 0;
}

Output

The final string is gffe

Time complexity – O(N) to traverse the array and string.

Space complexity – O(N) to store the elements in the prefix array.

We used two approaches to find the resultant string after incrementing the string prefix characters. The first approach traverses each array element and updates the string prefix. The second approach is more logical as we create the prefix array and increment characters simultaneously rather than incrementing multiple times in each operation.

Updated on: 31-Aug-2023

59 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements