# Minimum length String with Sum of the alphabetical values of the characters equal to N

Given an integer N, the task is to find the minimum length string whose sum of each character (As a = 1, b = 2, … z = 26) equals to N.

Examples:

```Input: N = 5
Output: e
5 can be represented as "aac" or "ad" or "e" etc
But we will take e as it is the minimum length

Input: N = 34
Output: zj
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

• To minimise the length of the String, Greedy Approach will be used.
• By Greedy Approach, the solution will be very simple.
• The minimum length of the String will be
```N/26 + 1 => if N % 26 != 0
N/26     => if N % 26 == 0
```
• And the minimum string can be found as
```(N/26 times z) + (N%26) => if N % 26 != 0
(N/26 times z)          => if N % 26 == 0
```

Below is the implementation of the above approach:

## C++

 `// C++ program to find the Minimum length String ` `// with Sum of the alphabetical values ` `// of the characters equal to N ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to find the minimum length ` `int` `minLength(``int` `n) ` `{ ` `    ``int` `ans = n / 26; ` `    ``if` `(n % 26 != 0) ` `        ``ans++; ` ` `  `    ``return` `ans; ` `} ` ` `  `// Function to find the minimum length String ` `string minString(``int` `n) ` `{ ` `    ``int` `ans = n / 26; ` `    ``string res = ``""``; ` ` `  `    ``while` `(ans--) { ` `        ``res = res + ``"z"``; ` `    ``} ` ` `  `    ``if` `(n % 26 != 0) { ` `        ``res = res ` `              ``+ (``char``)((n % 26) + 96); ` `    ``} ` ` `  `    ``return` `res; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `n = 50; ` ` `  `    ``cout << minLength(n) ` `         ``<< endl ` `         ``<< minString(n); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to find the Minimum length String  ` `// with Sum of the alphabetical values  ` `// of the characters equal to N  ` `class` `GFG  ` `{ ` ` `  `    ``// Function to find the minimum length  ` `    ``static` `int` `minLength(``int` `n)  ` `    ``{  ` `        ``int` `ans = n / ``26``;  ` `        ``if` `(n % ``26` `!= ``0``)  ` `            ``ans++;  ` `     `  `        ``return` `ans;  ` `    ``}  ` `     `  `    ``// Function to find the minimum length String  ` `    ``static` `String minString(``int` `n)  ` `    ``{  ` `        ``int` `ans = n / ``26``;  ` `        ``String res = ``""``;  ` `     `  `        ``while` `(ans-- != ``0``) ` `        ``{  ` `            ``res = res + ``"z"``;  ` `        ``}  ` `     `  `        ``if` `(n % ``26` `!= ``0``)  ` `        ``{  ` `            ``res = res + (``char``)((n % ``26``) + ``96``);  ` `        ``}  ` `     `  `        ``return` `res;  ` `    ``}  ` `     `  `    ``// Driver code  ` `    ``public` `static` `void` `main (String[] args) ` `    ``{  ` `        ``int` `n = ``50``;  ` `     `  `        ``System.out.println(minLength(n)); ` `        ``System.out.println(minString(n));  ` `    ``}  ` `} ` ` `  `// This code is contributed by AnkitRai01 `

## Python3

 `# Python3 program to find the Minimum length String ` `# with Sum of the alphabetical values ` `# of the characters equal to N ` ` `  `# Function to find the minimum length ` `def` `minLength(n): ` `    ``ans ``=` `n ``/``/` `26` `    ``if` `(n ``%` `26` `!``=` `0``): ` `        ``ans ``+``=` `1` ` `  `    ``return` `ans ` ` `  `# Function to find the minimum length String ` `def` `minString(n): ` `    ``ans ``=` `n ``/``/` `26` `    ``res ``=` `"" ` ` `  `    ``while` `(ans): ` `        ``res ``=` `res ``+` `"z"` `        ``ans``-``=``1` ` `  `    ``if` `(n ``%` `26` `!``=` `0``): ` `        ``res ``=` `res ``+` `chr``((n ``%` `26``) ``+` `96``) ` ` `  `    ``return` `res ` ` `  `# Driver code ` `n ``=` `50``; ` ` `  `print``(minLength(n)) ` `print``(minString(n)) ` ` `  `# This code is contributed by Mohit Kumar `

## C#

 `// C# iprogram to find the Minimum length String  ` `// with Sum of the alphabetical values  ` `// of the characters equal to N  ` `using` `System; ` `     `  `class` `GFG  ` `{ ` ` `  `    ``// Function to find the minimum length  ` `    ``static` `int` `minLength(``int` `n)  ` `    ``{  ` `        ``int` `ans = n / 26;  ` `        ``if` `(n % 26 != 0)  ` `            ``ans++;  ` `     `  `        ``return` `ans;  ` `    ``}  ` `     `  `    ``// Function to find the minimum length String  ` `    ``static` `String minString(``int` `n)  ` `    ``{  ` `        ``int` `ans = n / 26;  ` `        ``String res = ``""``;  ` `     `  `        ``while` `(ans-- != 0) ` `        ``{  ` `            ``res = res + ``"z"``;  ` `        ``}  ` `     `  `        ``if` `(n % 26 != 0)  ` `        ``{  ` `            ``res = res + (``char``)((n % 26) + 96);  ` `        ``}  ` `        ``return` `res;  ` `    ``}  ` `     `  `    ``// Driver code  ` `    ``public` `static` `void` `Main (String[] args) ` `    ``{  ` `        ``int` `n = 50;  ` `     `  `        ``Console.WriteLine(minLength(n)); ` `        ``Console.WriteLine(minString(n));  ` `    ``}  ` `} ` ` `  `// This code is contributed by PrinciRaj1992 `

Output:

```2
zx
```

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.