1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-10 18:48:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/形成目标字符串需要的最少字符串数 II [minimum-number-of-valid-strings-to-form-target-ii].html
2024-09-19 09:27:23 +08:00

64 lines
2.5 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>给你一个字符串数组 <code>words</code> 和一个字符串 <code>target</code></p>
<p>如果字符串 <code>x</code><code>words</code><strong> 任意 </strong>字符串的 <span data-keyword="string-prefix">前缀</span>,则认为 <code>x</code> 是一个 <strong>有效</strong> 字符串。</p>
<p>现计划通过 <strong>连接 </strong>有效字符串形成 <code>target</code> ,请你计算并返回需要连接的 <strong>最少 </strong>字符串数量。如果无法通过这种方式形成 <code>target</code>,则返回 <code>-1</code></p>
<p>&nbsp;</p>
<p><strong class="example">示例 1</strong></p>
<div class="example-block">
<p><strong>输入:</strong> <span class="example-io">words = ["abc","aaaaa","bcdef"], target = "aabcdabc"</span></p>
<p><strong>输出:</strong> <span class="example-io">3</span></p>
<p><strong>解释:</strong></p>
<p>target 字符串可以通过连接以下有效字符串形成:</p>
<ul>
<li><code>words[1]</code> 的长度为 2 的前缀,即 <code>"aa"</code></li>
<li><code>words[2]</code> 的长度为 3 的前缀,即 <code>"bcd"</code></li>
<li><code>words[0]</code> 的长度为 3 的前缀,即 <code>"abc"</code></li>
</ul>
</div>
<p><strong class="example">示例 2</strong></p>
<div class="example-block">
<p><strong>输入:</strong> <span class="example-io">words = ["abababab","ab"], target = "ababaababa"</span></p>
<p><strong>输出:</strong> <span class="example-io">2</span></p>
<p><strong>解释:</strong></p>
<p>target 字符串可以通过连接以下有效字符串形成:</p>
<ul>
<li><code>words[0]</code> 的长度为 5 的前缀,即 <code>"ababa"</code></li>
<li><code>words[0]</code> 的长度为 5 的前缀,即 <code>"ababa"</code></li>
</ul>
</div>
<p><strong class="example">示例 3</strong></p>
<div class="example-block">
<p><strong>输入:</strong> <span class="example-io">words = ["abcdef"], target = "xyz"</span></p>
<p><strong>输出:</strong> <span class="example-io">-1</span></p>
</div>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= words.length &lt;= 100</code></li>
<li><code>1 &lt;= words[i].length &lt;= 5 * 10<sup>4</sup></code></li>
<li>输入确保 <code>sum(words[i].length) &lt;= 10<sup>5</sup></code>.</li>
<li><code>words[i]</code> &nbsp;只包含小写英文字母。</li>
<li><code>1 &lt;= target.length &lt;= 5 * 10<sup>4</sup></code></li>
<li><code>target</code> &nbsp;只包含小写英文字母。</li>
</ul>