The numeric value of a lowercase character is defined as its position (1-indexed)
in the alphabet, so the numeric value of a
is 1
, the numeric value of b
is 2
, the numeric value of c
is 3
, and so on.
The numeric value of a string consisting of lowercase characters is defined as the sum of its characters' numeric values. For example, the numeric value of the string "abe"
is equal to 1 + 2 + 5 = 8
.
You are given two integers n
and k
. Return the lexicographically smallest string with length equal to n
and numeric value equal to k
.
Note that a string x
is lexicographically smaller than string y
if x
comes before y
in dictionary order, that is, either x
is a prefix of y
, or if i
is the first position such that x[i] != y[i]
, then x[i]
comes before y[i]
in alphabetic order.
Example 1:
Input: n = 3, k = 27 Output: "aay" Explanation: The numeric value of the string is 1 + 1 + 25 = 27, and it is the smallest string with such a value and length equal to 3.
Example 2:
Input: n = 5, k = 73 Output: "aaszz"
Constraints:
1 <= n <= 105
n <= k <= 26 * n
Companies:
Lendingkart
Related Topics:
String, Greedy
Intuition: We can do it greedily:
- If picking
a
won't result in unsolvable problem, we prependa
to the start of the string. - Otherwise, if picking
z
won't result in unsolvable problem, we appendz
to the end of the string. - Otherwise, if there is still a non-zero
k
value left (which must be smaller than 26), we add'a' + k - 1
in the middle.
Algorithm:
How to check if it will become unsolvable problem?
If after picking 'a'
, the remainder numeric value k - 1
can't be formed even if using all 'z'
s, i.e. k - 1 >= (n - 1) * 26
, then it's unsolvable.
So if n - 1 > 0 && k - 1 < (n - 1) * 26
, we should keep prepending 'a'
.
If after picking 'z'
, the remainder numeric value k - 26
can't be formed even if using all 'a'
s, i.e. k - 26 < n - 1
, then it's unsolvable.
So if n - 1 > 0 && k - 26 >= n - 1
, we should keep appending 'z'
.
// OJ: https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(1)
class Solution {
public:
string getSmallestString(int n, int k) {
string s;
int a = 0, z = 0;
while (n - 1 > 0 && k - 1 < (n - 1) * 26) ++a, --k, --n;
while (n - 1 > 0 && k - 26 >= n - 1) ++z, k -= 26, --n;
while (a-- > 0) s += 'a';
if (k) s += 'a' + k - 1;
while (z-- > 0) s += 'z';
return s;
}
};
Starts with all 'a'
, fill the k
value from the end of the array backwards.
// OJ: https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(1)
class Solution {
public:
string getSmallestString(int n, int k) {
string s(n, 'a');
k -= n;
for (int i = n - 1; k > 0; --i) {
int d = min(k, 25);
s[i] += d;
k -= d;
}
return s;
}
};
Initialize ans
as all z
s. Try filling a
from the left. If we fill a
at and before index i
, we have k - i - 1
values left which should be <= 26 * (n - i - 1)
i.e. the value of all the z
s to the right of i
. So, we keep filling a
when k - i - 1 <= 26 * (n - i - 1)
.
When we exit the loop, ans[i]
might not be z
. The value left for ans[i]
is k
minus the values created by the a
s to the left, i.e. i
, and minus the values created by the z
s to the right, i.e. 26 * (n - i - 1)
. So, the value for ans[i]
is k - i - (n - i - 1) * 26
.
// OJ: https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(1)
class Solution {
public:
string getSmallestString(int n, int k) {
string ans(n, 'z');
int i = 0;
for (; i < n && k - i - 1 <= 26 * (n - i - 1); ++i) {
ans[i] = 'a';
}
ans[i] = 'a' + k - i - 1 - (n - i - 1) * 26;
return ans;
}
};