给定一个正整数N ,任务是找到由小写英文字母组成的字典序最大的字符串,使得字符串的字符总和等于N其中‘a’ = 1 , ‘b’ = 2 , ‘c’ = 3 , ….. 和‘z’ = 26 。
例子:
Input: N = 30
Output: zd
Explanation:
The lexicographically largest string formed is “zd” whose sum of position of characters is (26 + 4) = 30(= N).
Input: N = 14
Output: n
方法:为了使字典顺序最大的字符串,这个想法是打印的字符Z,N / 26的次数,然后在(N%26 + 1)的英文字母位置i的字符。请按照以下步骤解决问题:
- 初始化一个字符串,比如ans ,它存储所需的字典序最大的字符串。
- 迭代直到N至少为26并执行以下步骤:
- 将字符z添加到字符串ans 。
- 将N的值减少26 。
- 将char(N + ‘a’)的值添加到字符串ans 。
- 完成上述步骤后,打印ans的值作为结果字符串。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to construct the
// lexicographically largest string
// having sum of characters as N
string getString(int N)
{
// Stores the resulting string
string ans = "";
// Iterate until N is at least 26
while (N >= 26) {
// Append 'z' to the string ans
ans += 'z';
// Decrement N by 26
N -= 26;
}
// Append character at index (N + 'a')
ans += char(N + 'a' - 1);
// Return the resultant string
return ans;
}
// Driver Code
int main()
{
int N = 30;
cout << getString(N);
return 0;
}
Java
// java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
public class GFG {
// Function to construct the
// lexicographically largest string
// having sum of characters as N
static String getString(int N)
{
// Stores the resulting string
String ans = "";
// Iterate until N is at least 26
while (N >= 26) {
// Append 'z' to the string ans
ans += 'z';
// Decrement N by 26
N -= 26;
}
// Append character at index (N + 'a')
ans += (char)(N + 'a' - 1);
// Return the resultant string
return ans;
}
// Driver Code
public static void main(String[] args)
{
int N = 30;
System.out.print(getString(N));
}
}
// This code is contributed by Kingash.
Python3
# Python3 program for the above approach
# Function to construct the
# lexicographically largest string
# having sum of characters as N
def getString(N):
# Stores the resulting string
ans = ""
# Iterate until N is at least 26
while (N >= 26):
# Append 'z' to the string ans
ans += 'z'
# Decrement N by 26
N -= 26
# Append character at index (N + 'a')
ans += chr(N + ord('a') - 1)
# Return the resultant string
return ans
# Driver Code
if __name__ == '__main__':
N = 30
print(getString(N))
# This code is contributed by mohit kumar 29.
C#
// C# program for the above approach
using System;
public class GFG {
// Function to construct the
// lexicographically largest string
// having sum of characters as N
static string getString(int N)
{
// Stores the resulting string
string ans = "";
// Iterate until N is at least 26
while (N >= 26) {
// Append 'z' to the string ans
ans += 'z';
// Decrement N by 26
N -= 26;
}
// Append character at index (N + 'a')
ans += (char)(N + 'a' - 1);
// Return the resultant string
return ans;
}
// Driver Code
public static void Main(string[] args)
{
int N = 30;
Console.WriteLine(getString(N));
}
}
// This code is contributed by ukasp.
Javascript
输出:
zd
时间复杂度: O(N)
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live