Welcome to Subscribe On Youtube
Formatted question description: https://leetcode.ca/all/1663.html
1663. Smallest String With A Given Numeric Value (Medium)
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
Related Topics:
Greedy
Solution 1. 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/
// 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;
}
};
Solution 2.
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/
// 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;
}
};
-
class Solution { public String getSmallestString(int n, int k) { int[] array = new int[n]; for (int i = 0; i < n; i++) { int remainLength = n - i - 1; int remainSum = 26 * remainLength; array[i] = Math.max(1, k - remainSum); k -= array[i]; } StringBuffer sb = new StringBuffer(); for (int i = 0; i < n; i++) { char c = (char) ('a' + array[i] - 1); sb.append(c); } return sb.toString(); } } ############ class Solution { public String getSmallestString(int n, int k) { char[] ans = new char[n]; Arrays.fill(ans, 'a'); int i = n - 1, d = k - n; for (; d > 25; d -= 25) { ans[i--] = 'z'; } ans[i] = (char) ('a' + d); return String.valueOf(ans); } }
-
// OJ: https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/ // 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; } };
-
class Solution: def getSmallestString(self, n: int, k: int) -> str: ans = ['a'] * n i, d = n - 1, k - n while d > 25: ans[i] = 'z' d -= 25 i -= 1 ans[i] = chr(ord(ans[i]) + d) return ''.join(ans) ############ # 1663. Smallest String With A Given Numeric Value # https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/ class Solution: def getSmallestString(self, n: int, k: int) -> str: res = [] while k > 0 and n > 0: if (26 + n) <= k: res.append('z') k -= 26 n -= 1 else: if n == 1: t = k % 27 res.append(chr(t+96)) k -= t n -= 1 else: t = (k-n+1) % 27 res.append(chr(t+96)) k -= t n -= 1 res.sort() return "".join(res)
-
func getSmallestString(n int, k int) string { ans := make([]byte, n) for i := range ans { ans[i] = 'a' } i, d := n-1, k-n for ; d > 25; i, d = i-1, d-25 { ans[i] = 'z' } ans[i] += byte(d) return string(ans) }