{"id":7699,"date":"2020-11-22T14:33:02","date_gmt":"2020-11-22T22:33:02","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7699"},"modified":"2020-11-22T14:35:07","modified_gmt":"2020-11-22T22:35:07","slug":"leetcode-1663-smallest-string-with-a-given-numeric-value","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-1663-smallest-string-with-a-given-numeric-value\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1663. Smallest String With A Given Numeric Value"},"content":{"rendered":"\n<p>The&nbsp;<strong>numeric value<\/strong>&nbsp;of a&nbsp;<strong>lowercase character<\/strong>&nbsp;is defined as its position&nbsp;<code>(1-indexed)<\/code>&nbsp;in the alphabet, so the numeric value of&nbsp;<code>a<\/code>&nbsp;is&nbsp;<code>1<\/code>, the numeric value of&nbsp;<code>b<\/code>&nbsp;is&nbsp;<code>2<\/code>, the numeric value of&nbsp;<code>c<\/code>&nbsp;is&nbsp;<code>3<\/code>, and so on.<\/p>\n\n\n\n<p>The&nbsp;<strong>numeric value<\/strong>&nbsp;of a&nbsp;<strong>string<\/strong>&nbsp;consisting of lowercase characters is defined as the sum of its characters&#8217; numeric values. For example, the numeric value of the string&nbsp;<code>\"abe\"<\/code>&nbsp;is equal to&nbsp;<code>1 + 2 + 5 = 8<\/code>.<\/p>\n\n\n\n<p>You are given two integers&nbsp;<code>n<\/code>&nbsp;and&nbsp;<code>k<\/code>. Return&nbsp;<em>the&nbsp;<strong>lexicographically smallest string<\/strong>&nbsp;with&nbsp;<strong>length<\/strong>&nbsp;equal to&nbsp;<code>n<\/code>&nbsp;and&nbsp;<strong>numeric value<\/strong>&nbsp;equal to&nbsp;<code>k<\/code>.<\/em><\/p>\n\n\n\n<p>Note that a string&nbsp;<code>x<\/code>&nbsp;is lexicographically smaller than string&nbsp;<code>y<\/code>&nbsp;if&nbsp;<code>x<\/code>&nbsp;comes before&nbsp;<code>y<\/code>&nbsp;in dictionary order, that is, either&nbsp;<code>x<\/code>&nbsp;is a prefix of&nbsp;<code>y<\/code>, or if&nbsp;<code>i<\/code>&nbsp;is the first position such that&nbsp;<code>x[i] != y[i]<\/code>, then&nbsp;<code>x[i]<\/code>&nbsp;comes before&nbsp;<code>y[i]<\/code>&nbsp;in alphabetic order.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 3, k = 27\n<strong>Output:<\/strong> \"aay\"\n<strong>Explanation:<\/strong> 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.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 5, k = 73\n<strong>Output:<\/strong> \"aaszz\"\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= n &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>n &lt;= k &lt;= 26 * n<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Greedy, Fill in reverse order<\/strong><\/h2>\n\n\n\n<p>Fill the entire string with &#8216;a&#8217;, k-=n, then fill in reverse order, replace &#8216;a&#8217; with &#8216;z&#8217; until not enough k left.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\/\/ Author: Huahua\nclass Solution {\npublic:\n  string getSmallestString(int n, int k) {    \n    string ans(n, 'a');\n    k -= n;\n    while (k) {\n      int d = min(k, 25);\n      ans[--n] += d;\n      k -= d;\n    }\n    return ans;\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">\n# Author: Huahua\nclass Solution:\n  def getSmallestString(self, n: int, k: int) -> str:\n    ans = ['a'] * n\n    k -= n\n    i = n - 1\n    while k:\n      d = min(k, 25)\n      ans[i] = chr(ord(ans[i]) + d)\n      k -= d\n      i -= 1\n    return ''.join(ans)\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>The&nbsp;numeric value&nbsp;of a&nbsp;lowercase character&nbsp;is defined as its position&nbsp;(1-indexed)&nbsp;in the alphabet, so the numeric value of&nbsp;a&nbsp;is&nbsp;1, the numeric value of&nbsp;b&nbsp;is&nbsp;2, the numeric value of&nbsp;c&nbsp;is&nbsp;3, and so&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[88,4],"class_list":["post-7699","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-greedy","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7699","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=7699"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7699\/revisions"}],"predecessor-version":[{"id":7701,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7699\/revisions\/7701"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7699"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7699"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7699"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}