📜  长度为 N 和和为 K 的字典序最小字符串

📅  最后修改于: 2021-10-25 10:36:18             🧑  作者: Mango

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

方法:

  • 初始化大小为N 的char 数组并用‘a’填充所有元素。
  • 从数组末尾开始遍历,如果K ≥ 26则将数组元素替换为‘z’或替换为具有 ASCII 值(K + 97 – 1)的字符。
  • 同时通过替换元素值来减小K的值,即对于a = 1 , b = 2 , c = 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


Javascript


输出:
aaamz

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程