Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Practice Mathematical Algorithm
  • Mathematical Algorithms
  • Pythagorean Triplet
  • Fibonacci Number
  • Euclidean Algorithm
  • LCM of Array
  • GCD of Array
  • Binomial Coefficient
  • Catalan Numbers
  • Sieve of Eratosthenes
  • Euler Totient Function
  • Modular Exponentiation
  • Modular Multiplicative Inverse
  • Stein's Algorithm
  • Juggler Sequence
  • Chinese Remainder Theorem
  • Quiz on Fibonacci Numbers
Open In App
Next Article:
Minimum value of K such that each substring of size K has the given character
Next article icon

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

Last Updated : 13 Mar, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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

 

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 <bits/stdc++.h>
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
 
 

Javascript




<script>
 
// Javascript 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
function minLength(n)
{
    var ans = parseInt(n / 26);
    if (n % 26 != 0)
        ans++;
 
    return ans;
}
 
// Function to find the minimum length String
function minString(n)
{
    var ans = parseInt(n / 26);
    var res = "";
 
    while (ans--) {
        res = res + "z";
    }
 
    if (n % 26 != 0) {
        res = res
              + String.fromCharCode((n % 26) + 96);
    }
 
    return res;
}
 
// Driver code
var n = 50;
document.write(minLength(n)+"<br>" + minString(n));
 
</script>
 
 
Output: 
2 zx

 

Time Complexity: O(x) where x = n/26

Auxiliary Space: O(x+1) where x = n/26



Next Article
Minimum value of K such that each substring of size K has the given character
author
spp____
Improve
Article Tags :
  • DSA
  • Greedy
  • Mathematical
  • Strings
Practice Tags :
  • Greedy
  • Mathematical
  • Strings

Similar Reads

  • Sum of the alphabetical values of the characters of a string
    You are given an array of strings str, the task is to find the score of a given string s from the array. The score of a string is defined as the product of the sum of its character alphabetical values with the position of the string in the array. Examples: Input: str[] = {"sahil", "shashanak", "sanj
    5 min read
  • Minimum length of the sub-string whose characters can be used to form a palindrome of length K
    Given a string str consisting of lowercase English letters and an integer K. The task is to find the minimum length of the sub-string whose characters can be used to form a palindrome of length K. If no such sub-string exists then print -1.Examples: Input: str = "abcda", k = 2 Output: 5 In order to
    14 min read
  • Find the sum of alphabetical order of characters in a string
    Given string S of size N, the task is to find the sum of the alphabet value of each character in the given string. Examples: Input: S = “geek” Output: 28 Explanation:The value obtained by the sum order of alphabets is 7 + 5 + 5 + 11 = 28. Input: S = “GeeksforGeeks” Output: 133 Approach: Traverse all
    3 min read
  • Minimum value of K such that each substring of size K has the given character
    Given a string of lowercase letters S a character c. The task is to find minimum K such that every substring of length K contains the given character c. If there is no such K possible, return -1.Examples: Input: S = "abdegb", ch = 'b'Output: 4 Explanation:Consider the value of K as 4. Now, every sub
    12 min read
  • Find the single digit sum of alphabetical values of a string
    Given string S of size N, the task is to find the single-digit sum by the repetitive sum of digits of the value obtained by the sum of order of all alphabets in the given string. The order of alphabets is given by the position at which they occur in English Alaphabets. Examples: Input: S = "geek"Out
    6 min read
  • Minimum K such that every substring of length atleast K contains a character c
    Given a string S containing lowercase latin letters. A character c is called K-amazing if every substring of S with length atleast K contains this character c. Find the minimum possible K such that there exists atleast one K-amazing character. Examples: Input : S = "abcde" Output :3 Explanation : Ev
    11 min read
  • Count of Substrings that can be formed without using the given list of Characters
    Given a string str and a list of characters L, the task is to count the total numbers of substrings of the string str without using characters given in the list L. Examples: Input: str = "abcd", L[] = {'a', 'b', 't', 'q'} Output: 3 Explanation: On ignoring the characters 'a' and 'b' from the given s
    7 min read
  • Lexicographically largest string with sum of characters equal to N
    Given a positive integer N, the task is to find the lexicographically largest string consisting of lower-case English alphabets such that the sum of the characters of the string equals N where ‘a’ = 1, ‘b’ = 2, ‘c’ = 3, ..... , and ‘z’ = 26. Examples: Input: N = 30Output: zdExplanation:The lexicogra
    4 min read
  • Minimum deletions from string to reduce it to string with at most 2 unique characters
    Given a string [Tex]S [/Tex]containing lowercase English alphabets. The task is to find the minimum number of characters needed to be removed so that the remaining string contains at most 2 unique characters. Note: The final string can have duplicate characters. The task is only to reduce the string
    5 min read
  • Minimum K such that every substring of length at least K contains a character c | Set-2
    Given a string S consisting of N lowercase English alphabets, and also given that a character C is called K-amazing, if every substring of length at least K contains this character C, the task is to find the minimum possible K such that there exists at least one K-amazing character. Examples: Input:
    8 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences