1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-27 02:30:28 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/最长公共后缀查询 [longest-common-suffix-queries].html

58 lines
4.3 KiB
HTML
Raw Normal View History

2024-03-28 09:27:27 +08:00
<p>给你两个字符串数组&nbsp;<code>wordsContainer</code>&nbsp;<code>wordsQuery</code>&nbsp;</p>
<p>对于每个&nbsp;<code>wordsQuery[i]</code>&nbsp;,你需要从&nbsp;<code>wordsContainer</code>&nbsp;中找到一个与&nbsp;<code>wordsQuery[i]</code>&nbsp;&nbsp;<strong>最长公共后缀</strong>&nbsp;的字符串。如果 <code>wordsContainer</code>&nbsp;中有两个或者更多字符串有最长公共后缀,那么答案为长度<strong>&nbsp;最短</strong>&nbsp;的。如果有超过两个字符串有&nbsp;<strong>相同</strong>&nbsp;最短长度,那么答案为它们在&nbsp;<code>wordsContainer</code>&nbsp;中出现&nbsp;<strong>更早</strong>&nbsp;的一个。</p>
<p>请你返回一个整数数组<em>&nbsp;</em><code>ans</code>&nbsp;,其中<em>&nbsp;</em><code>ans[i]</code><em>&nbsp;</em><code>wordsContainer</code>中与&nbsp;<code>wordsQuery[i]</code>&nbsp;&nbsp;<strong>最长公共后缀</strong>&nbsp;字符串的下标。</p>
<p>&nbsp;</p>
<p><strong class="example">示例 1</strong></p>
<div class="example-block">
<p><span class="example-io"><b>输入:</b>wordsContainer = ["abcd","bcd","xbcd"], wordsQuery = ["cd","bcd","xyz"]</span></p>
<p><span class="example-io"><b>输出:</b>[1,1,1]</span></p>
<p><strong>解释:</strong></p>
<p>我们分别来看每一个&nbsp;<code>wordsQuery[i]</code>&nbsp;</p>
<ul>
<li>对于&nbsp;<code>wordsQuery[0] = "cd"</code>&nbsp;<code>wordsContainer</code>&nbsp;中有最长公共后缀&nbsp;<code>"cd"</code>&nbsp;的字符串下标分别为&nbsp;0 1 和&nbsp;2 。这些字符串中,答案是下标为 1 的字符串,因为它的长度为 3 ,是最短的字符串。</li>
<li>对于&nbsp;<code>wordsQuery[1] = "bcd"</code>&nbsp;<code>wordsContainer</code>&nbsp;中有最长公共后缀&nbsp;<code>"bcd"</code>&nbsp;的字符串下标分别为 0 1 和 2 。这些字符串中,答案是下标为 1 的字符串,因为它的长度为 3 ,是最短的字符串。</li>
<li>对于&nbsp;<code>wordsQuery[2] = "xyz"</code>&nbsp;<code>wordsContainer</code>&nbsp;中没有字符串跟它有公共后缀,所以最长公共后缀为&nbsp;<code>""</code>&nbsp;,下标为&nbsp;0 1 和 2 的字符串都得到这一公共后缀。这些字符串中,&nbsp;答案是下标为 1 的字符串,因为它的长度为 3 ,是最短的字符串。</li>
</ul>
</div>
<p><strong class="example">示例 2</strong></p>
<div class="example-block">
<p><span class="example-io"><b>输入:</b>wordsContainer = ["abcdefgh","poiuygh","ghghgh"], wordsQuery = ["gh","acbfgh","acbfegh"]</span></p>
<p><span class="example-io"><b>输出:</b>[2,0,2]</span></p>
<p><strong>解释:</strong></p>
<p>我们分别来看每一个&nbsp;<code>wordsQuery[i]</code>&nbsp;</p>
<ul>
<li>对于&nbsp;<code>wordsQuery[0] = "gh"</code>&nbsp;<code>wordsContainer</code>&nbsp;中有最长公共后缀&nbsp;<code>"gh"</code>&nbsp;的字符串下标分别为 0 1 和 2 。这些字符串中,答案是下标为 2 的字符串,因为它的长度为 6 ,是最短的字符串。</li>
<li>对于&nbsp;<code>wordsQuery[1] = "acbfgh"</code>&nbsp;,只有下标为 0 的字符串有最长公共后缀&nbsp;<code>"fgh"</code>&nbsp;。所以尽管下标为 2 的字符串是最短的字符串,但答案是 0 。</li>
<li>对于&nbsp;<code>wordsQuery[2] = "acbfegh"</code>&nbsp;<code>wordsContainer</code>&nbsp;中有最长公共后缀&nbsp;<code>"gh"</code>&nbsp;的字符串下标分别为 0 1 和 2 。这些字符串中,答案是下标为 2 的字符串,因为它的长度为 6 ,是最短的字符串。</li>
</ul>
</div>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= wordsContainer.length, wordsQuery.length &lt;= 10<sup>4</sup></code></li>
<li><code>1 &lt;= wordsContainer[i].length &lt;= 5 * 10<sup>3</sup></code></li>
<li><code>1 &lt;= wordsQuery[i].length &lt;= 5 * 10<sup>3</sup></code></li>
<li><code>wordsContainer[i]</code>&nbsp;只包含小写英文字母。</li>
<li><code>wordsQuery[i]</code>&nbsp;只包含小写英文字母。</li>
<li><code>wordsContainer[i].length</code>&nbsp;的和至多为&nbsp;<code>5 * 10<sup>5</sup></code>&nbsp;</li>
<li><code>wordsQuery[i].length</code>&nbsp;的和至多为&nbsp;<code>5 * 10<sup>5</sup></code>&nbsp;</li>
</ul>