📜  字法上最小的长度为N且总和为K的字符串

📅  最后修改于: 2021-04-23 16:54:25             🧑  作者: Mango

给定两个整数NK。该任务是打印由小写的英语字母组成的长度为N的词典上最小的字符串,以使字符串的字符总和等于K ,其中‘a’= 1,’b’= 2,’c’= 3 ,…..和’z’= 26

例子:

方法:

  • 初始化大小为N的char数组,并用‘a’填充所有元素。
  • 从数组的末尾开始遍历,如果K≥26 ,则用‘z’替换数组的元素,或者用ASCII值(K + 97-1)的字符替换它。
  • 同时,用替换的元素值减小K的值,即a = 1b = 2c = 3 ,…, z = 26
  • 另外,请注意,我们要减去当前元素之前的前一个元素值,即(总“ a”),并在for循环结束之前添加该值。
  • 检查K <0条件并中断for循环。
  • 返回由char数组的元素形成的新字符串作为答案。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include
using namespace std;
  
// Function to return the lexicographically
// smallest string of length n that
// satisfies the given condition
string lexo_small(int n, int k)
{
    string arr = "";
  
    for(int i = 0; i < n; i++)
        arr += 'a';
  
    // Iteration from the last position
    // in the array
    for (int i = n - 1; i >= 0; i--)
    {
        k -= i;
  
        // If k is a positive integer
        if (k >= 0)
        {
  
            // 'z' needs to be inserted
            if (k >= 26)
            {
                arr[i] = 'z';
                k -= 26;
            }
  
            // Add the required character
            else
            {
                char c= (char)(k + 97 - 1);
                arr[i] = c;
                k -= arr[i] - 'a' + 1;
            }
        }
  
        else
            break;
  
        k += i;
    }
    return arr;
}
  
// Driver code
int main()
{
    int n = 5, k = 42;
  
    string arr = lexo_small(n, k);
  
    cout << arr;
}
  
// This code is contributed by Mohit Kumar


Java
// Java implementation of the approach
import java.util.Arrays;
  
public class Main {
  
    // Function to return the lexicographically
    // smallest string of length n that
    // satisfies the given condition
    public static char[] lexo_small(int n, int k)
    {
        char arr[] = new char[n];
  
        Arrays.fill(arr, 'a');
  
        // Iteration from the last position
        // in the array
        for (int i = n - 1; i >= 0; i--) {
  
            k -= i;
  
            // If k is a positive integer
            if (k >= 0) {
  
                // 'z' needs to be inserted
                if (k >= 26) {
                    arr[i] = 'z';
                    k -= 26;
                }
  
                // Add the required character
                else {
                    arr[i] = (char)(k + 97 - 1);
                    k -= arr[i] - 'a' + 1;
                }
            }
  
            else
                break;
  
            k += i;
        }
  
        return arr;
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int n = 5, k = 42;
  
        char arr[] = lexo_small(n, k);
  
        System.out.print(new String(arr));
    }
}


Python3
# Python implementation of the approach
  
# Function to return the lexicographically
# smallest string of length n that
# satisfies the given condition
def lexo_small(n, k):
  
    arr = "";
  
    for i in range(n):
        arr += 'a';
  
    # Iteration from the last position
    # in the array
    for i in range(n-1,-1,-1):
        k -= i;
  
        # If k is a positive integer
        if (k >= 0):
  
            # 'z' needs to be inserted
            if (k >= 26):
                arr = arr[:i] + 'z' + arr[i+1:];
                k -= 26;
          
            # Add the required character
            else:
                c= (k + 97 - 1);
                arr = arr[:i] + chr(c) + arr[i+1:];
                k -= ord(arr[i]) - ord('a') + 1;
  
        else:
            break;
  
        k += i;
    return arr;
  
# Driver code
if __name__ == '__main__':
    n = 5; k = 42;
  
    arr = lexo_small(n, k);
  
    print(arr);
  
# This code contributed by PrinciRaj1992


C#
// C# implementation of the approach 
using System;
  
class GFG
{ 
  
    // Function to return the lexicographically 
    // smallest string of length n that 
    // satisfies the given condition 
    public static char[] lexo_small(int n, int k) 
    { 
        char []arr = new char[n]; 
        int i;
          
        for(i = 0; i < n; i++)
            arr[i] = 'a' ;
  
        // Iteration from the last position 
        // in the array 
        for (i = n - 1; i >= 0; i--) 
        { 
            k -= i; 
  
            // If k is a positive integer 
            if (k >= 0) 
            { 
  
                // 'z' needs to be inserted 
                if (k >= 26) 
                { 
                    arr[i] = 'z'; 
                    k -= 26; 
                } 
  
                // Add the required character 
                else
                { 
                    arr[i] = (char)(k + 97 - 1); 
                    k -= arr[i] - 'a' + 1; 
                } 
            } 
  
            else
                break; 
  
            k += i; 
        } 
        return arr; 
    } 
  
    // Driver code 
    public static void Main() 
    { 
        int n = 5, k = 42; 
  
        char []arr = lexo_small(n, k); 
  
        Console.WriteLine(new string(arr)); 
    } 
}
  
// This code is contributed by AnkitRai01


输出:
aaamz