1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-02-05 07:00:25 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/最短且字典序最小的美丽子字符串 [shortest-and-lexicographically-smallest-beautiful-string].html

63 lines
2.7 KiB
HTML
Raw Normal View History

2023-10-15 20:45:13 +08:00
<p>给你一个二进制字符串 <code>s</code> 和一个正整数 <code>k</code></p>
<p>如果 <code>s</code> 的某个子字符串中 <code>1</code> 的个数恰好等于 <code>k</code> ,则称这个子字符串是一个 <strong>美丽子字符串</strong></p>
<p><code>len</code> 等于 <strong>最短</strong> 美丽子字符串的长度。</p>
<p>返回长度等于 <code>len</code> 且字典序 <strong>最小</strong> 的美丽子字符串。如果 <code>s</code> 中不含美丽子字符串,则返回一个 <strong></strong> 字符串。</p>
<p>对于相同长度的两个字符串 <code>a</code><code>b</code> ,如果在 <code>a</code><code>b</code> 出现不同的第一个位置上,<code>a</code> 中该位置上的字符严格大于 <code>b</code> 中的对应字符,则认为字符串 <code>a</code> 字典序 <strong>大于</strong> 字符串 <code>b</code></p>
<ul>
<li>例如,<code>"abcd"</code> 的字典序大于 <code>"abcc"</code> ,因为两个字符串出现不同的第一个位置对应第四个字符,而 <code>d</code> 大于 <code>c</code></li>
</ul>
<p>&nbsp;</p>
<p><strong class="example">示例 1</strong></p>
<pre>
<strong>输入:</strong>s = "100011001", k = 3
<strong>输出:</strong>"11001"
<strong>解释:</strong>示例中共有 7 个美丽子字符串:
1. 子字符串 "<em><strong>100011</strong></em>001" 。
2. 子字符串 "<strong><em>1000110</em></strong>01" 。
3. 子字符串 "<strong><em>100011001</em></strong>" 。
4. 子字符串 "1<strong><em>00011001</em></strong>" 。
5. 子字符串 "10<strong><em>0011001</em></strong>" 。
6. 子字符串 "100<em><strong>011001</strong></em>" 。
7. 子字符串 "1000<strong><em>11001</em></strong>" 。
最短美丽子字符串的长度是 5 。
长度为 5 且字典序最小的美丽子字符串是子字符串 "11001" 。
</pre>
<p><strong class="example">示例 2</strong></p>
<pre>
<strong>输入:</strong>s = "1011", k = 2
<strong>输出:</strong>"11"
<strong>解释:</strong>示例中共有 3 个美丽子字符串:
1. 子字符串 "<em><strong>101</strong></em>1" 。
2. 子字符串 "1<em><strong>011</strong></em>" 。
3. 子字符串 "10<em><strong>11</strong></em>" 。
最短美丽子字符串的长度是 2 。
长度为 2 且字典序最小的美丽子字符串是子字符串 "11" 。
</pre>
<p><strong class="example">示例 3</strong></p>
<pre>
<strong>输入:</strong>s = "000", k = 1
<strong>输出:</strong>""
<strong>解释:</strong>示例中不存在美丽子字符串。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= s.length &lt;= 100</code></li>
<li><code>1 &lt;= k &lt;= s.length</code></li>
</ul>