|
| 1 | +# [1663. Smallest String With A Given Numeric Value](https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/) |
| 2 | + |
| 3 | +## 题目 |
| 4 | + |
| 5 | +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. |
| 6 | + |
| 7 | +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`. |
| 8 | + |
| 9 | +You are given two integers `n` and `k`. Return *the **lexicographically smallest string** with **length** equal to `n` and **numeric value** equal to `k`.* |
| 10 | + |
| 11 | +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. |
| 12 | + |
| 13 | +**Example 1:** |
| 14 | + |
| 15 | +``` |
| 16 | +Input: n = 3, k = 27 |
| 17 | +Output: "aay" |
| 18 | +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. |
| 19 | +``` |
| 20 | + |
| 21 | +**Example 2:** |
| 22 | + |
| 23 | +``` |
| 24 | +Input: n = 5, k = 73 |
| 25 | +Output: "aaszz" |
| 26 | +``` |
| 27 | + |
| 28 | +**Constraints:** |
| 29 | + |
| 30 | +- `1 <= n <= 105` |
| 31 | +- `n <= k <= 26 * n` |
| 32 | + |
| 33 | +## 题目大意 |
| 34 | + |
| 35 | +小写字符 的 数值 是它在字母表中的位置(从 1 开始),因此 a 的数值为 1 ,b 的数值为 2 ,c 的数值为 3 ,以此类推。字符串由若干小写字符组成,字符串的数值 为各字符的数值之和。例如,字符串 "abe" 的数值等于 1 + 2 + 5 = 8 。给你两个整数 n 和 k 。返回 长度 等于 n 且 数值 等于 k 的 字典序最小 的字符串。注意,如果字符串 x 在字典排序中位于 y 之前,就认为 x 字典序比 y 小,有以下两种情况: |
| 36 | + |
| 37 | +- x 是 y 的一个前缀; |
| 38 | +- 如果 i 是 x[i] != y[i] 的第一个位置,且 x[i] 在字母表中的位置比 y[i] 靠前。 |
| 39 | + |
| 40 | +## 解题思路 |
| 41 | + |
| 42 | +- 给出 n 和 k,要求找到字符串长度为 n,字母在字母表内位置总和为 k 的最小字典序字符串。 |
| 43 | +- 这一题笔者读完题,比赛的时候直接用 DFS 撸了一版。赛后看了时间复杂度马马虎虎,感觉还有优化的空间。DFS 会遍历出所有的解,实际上这一题只要求最小字典序,所以 DFS 剪枝的时候要加上判断字典序的判断,如果新添加进来的字母比已经保存的字符串的相应位置上的字母字典序大,那么就直接 return,这个答案一定不会是最小字典序。代码见解法二 |
| 44 | +- 想到这里,其实 DFS 不必要,直接用 for 循环就可找到最小字典序的字符串。代码见解法一。 |
| 45 | + |
| 46 | +## 代码 |
| 47 | + |
| 48 | +```go |
| 49 | +package leetcode |
| 50 | + |
| 51 | +// 解法一 贪心 |
| 52 | +func getSmallestString(n int, k int) string { |
| 53 | + res := make([]rune, n) |
| 54 | + for i := n - 1; i >= 0; i-- { |
| 55 | + diff := k - i |
| 56 | + if diff >= 26 { |
| 57 | + // Need to add z |
| 58 | + res[i] = 'z' |
| 59 | + k = k - 26 |
| 60 | + } else { |
| 61 | + res[i] = rune('a' + diff - 1) |
| 62 | + k = k - diff |
| 63 | + } |
| 64 | + } |
| 65 | + return string(res) |
| 66 | +} |
| 67 | + |
| 68 | +// 解法二 DFS |
| 69 | +func getSmallestString1(n int, k int) string { |
| 70 | + if n == 0 { |
| 71 | + return "" |
| 72 | + } |
| 73 | + res, c := "", []byte{} |
| 74 | + findSmallestString(0, n, k, 0, c, &res) |
| 75 | + return res |
| 76 | +} |
| 77 | + |
| 78 | +func findSmallestString(value int, length, k, index int, str []byte, res *string) { |
| 79 | + if len(str) == length && value == k { |
| 80 | + tmp := string(str) |
| 81 | + if (*res) == "" { |
| 82 | + *res = tmp |
| 83 | + } |
| 84 | + if tmp < *res && *res != "" { |
| 85 | + *res = tmp |
| 86 | + } |
| 87 | + return |
| 88 | + } |
| 89 | + if len(str) >= index && (*res) != "" && str[index-1] > (*res)[index-1] { |
| 90 | + return |
| 91 | + } |
| 92 | + for j := 0; j < 26; j++ { |
| 93 | + if k-value > (length-len(str))*26 || value > k { |
| 94 | + return |
| 95 | + } |
| 96 | + str = append(str, byte(int('a')+j)) |
| 97 | + value += j + 1 |
| 98 | + findSmallestString(value, length, k, index+1, str, res) |
| 99 | + str = str[:len(str)-1] |
| 100 | + value -= j + 1 |
| 101 | + |
| 102 | + } |
| 103 | +} |
| 104 | +``` |
0 commit comments