Lexicographically Largest String With Sum Of Characters Equal To N


Problem Statement

We have given a positive integer num. We need to find the lexicographically largest string of lowercase alphabetical characters such that the sum of all characters of the string is equal to num. Here, ‘a’ = 1, ‘b’ = 2, ‘c’ = 3, ‘d’ = 4, …., ‘z’ = 26.

We need to use the ‘z’ characters at the string's start to create the largest lexicographical string. At last, we need to use the last character according to the num % 26 value.

Sample Examples

Input

num = 30

Output

‘zd’

Explanation

The ‘zd’ is the largest lexicographical string whose characters sum is 30 (z = 26 + d = 4).

Input

3

Output

‘c’

Explanation

The ‘c’ represents 3 itself.

Input

130

Output

‘zzzzz’

Explanation

The sum of values respected to each character of ‘zzzzz’ is 130.

Approach 1

This approach will use a while loop to create a resultant string. We will make iterations until the value of a number is greater than or equal to 26, and in every iteration, we will add ‘z’ to the string and decrease the number by 26. At last, we will add a character to the string respected to the reminder.

Algorithm

  • Step 1 − execute the findString() function by passing the number value as a parameter.

  • Step 2 − Initialize the result variable of string type with an empty string to store the resultant string.

  • Step 3 − Use the while loop to make iterations until the value of ‘num’ is greater than or equal to 26.

  • Step 4 − In the while loop, append the ‘z’ character to the result string.

  • Step 5 − Decrease the value of a number by 26.

  • Step 6 − When the while loop iteration is complete, check if the value of num is greater than 0. If yes, append the last character to the string according to the ‘num’ variable value.

  • Step 7 − Return the resultant string.

Example

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

// function to find the resultant string
string findString(int num) {
   // variable to store the resultant string
   string result = "";
   // using a while loop to find the resultant string
   while (num >= 26) {
      // append z to the resultant string
      result += 'z';
      // Decrease the number by 26
      num -= 26;
   }
   // Convert the remaining number to char and append to the resultant string
   if(num != 0) {
      result += char(num + 'a' - 1);
   }
   return result;
}

int main() {
   int num = 96;
   cout << "The resultant string is " << findString(num);
   return 0;
}

Output

The resultant string is zzzr
  • Time complexity − O(num), as while loop runs the num/26 times, equal to the O(num).

  • Space complexity − O(num), as string can contain at most (num/26 + 1) characters.

Approach 2

In this approach, we will use the String() constructor to create a string of length N. We will use the modulo and division operators to get the total number of z’s in the string.

Algorithm

  • Step 1 − Define the ‘totalZ’ variable and initialize it with the num/26.

  • Step 2 − Define the ‘rem’ variable, and initialize it with the ‘num%26’.

  • Step 3 − Use the string() constructor by passing the ‘totalZ’ as a first parameter and ‘z’ as a second parameter, and it will create a string containing the totalZ number of ‘z’ characters. Also, append it to the ‘result’ string.

  • Step 4 − If the value of ‘rem’ is not equal to zero, append the last character to the string according to the ‘rem’ variable’s value.

  • Step 5 − Return the ‘result’ string.

Example

#include <bits/stdc++.h>
using namespace std;
// function to find the resultant string
string findString(int num) {
   // variable to store the resultant string
   string result = "";
   // variable to store the number of z's
   int totalZ = num / 26;
   // variable to store the remainder
   int rem = num % 26;
   // Using the string constructor to create a string with total number of totalZ 'z'.
   result += string(totalZ, 'z');
   // If the remainder is non-zero, then add the corresponding character
   if(rem != 0) {
      result += char(rem + 'a' - 1);
   }
   return result;
}
int main(){
   int num = 52;
   cout << "The resultant string is " << findString(num);
   return 0;
}

Output

The resultant string is zz
  • Time complexity − O(num), as a string constructor, creates a string containing totalz characters.

  • Space complexity − O(num)

Conclusion

We learned two approaches to converting numbers to strings. We used the while loop in the first approach and the string() constructor in the second approach. However, both approaches have the same space and time complexity, but the second approach is more readable.

Updated on: 18-Jul-2023

292 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements