mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
49 lines
2.6 KiB
HTML
49 lines
2.6 KiB
HTML
<p>Given two arrays of strings <code>list1</code> and <code>list2</code>, find the <strong>common strings with the least index sum</strong>.</p>
|
|
|
|
<p>A <strong>common string</strong> is a string that appeared in both <code>list1</code> and <code>list2</code>.</p>
|
|
|
|
<p>A <strong>common string with the least index sum</strong> is a common string such that if it appeared at <code>list1[i]</code> and <code>list2[j]</code> then <code>i + j</code> should be the minimum value among all the other <strong>common strings</strong>.</p>
|
|
|
|
<p>Return <em>all the <strong>common strings with the least index sum</strong></em>. Return the answer in <strong>any order</strong>.</p>
|
|
|
|
<p> </p>
|
|
<p><strong class="example">Example 1:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> list1 = ["Shogun","Tapioca Express","Burger King","KFC"], list2 = ["Piatti","The Grill at Torrey Pines","Hungry Hunter Steakhouse","Shogun"]
|
|
<strong>Output:</strong> ["Shogun"]
|
|
<strong>Explanation:</strong> The only common string is "Shogun".
|
|
</pre>
|
|
|
|
<p><strong class="example">Example 2:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> list1 = ["Shogun","Tapioca Express","Burger King","KFC"], list2 = ["KFC","Shogun","Burger King"]
|
|
<strong>Output:</strong> ["Shogun"]
|
|
<strong>Explanation:</strong> The common string with the least index sum is "Shogun" with index sum = (0 + 1) = 1.
|
|
</pre>
|
|
|
|
<p><strong class="example">Example 3:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> list1 = ["happy","sad","good"], list2 = ["sad","happy","good"]
|
|
<strong>Output:</strong> ["sad","happy"]
|
|
<strong>Explanation:</strong> There are three common strings:
|
|
"happy" with index sum = (0 + 1) = 1.
|
|
"sad" with index sum = (1 + 0) = 1.
|
|
"good" with index sum = (2 + 2) = 4.
|
|
The strings with the least index sum are "sad" and "happy".
|
|
</pre>
|
|
|
|
<p> </p>
|
|
<p><strong>Constraints:</strong></p>
|
|
|
|
<ul>
|
|
<li><code>1 <= list1.length, list2.length <= 1000</code></li>
|
|
<li><code>1 <= list1[i].length, list2[i].length <= 30</code></li>
|
|
<li><code>list1[i]</code> and <code>list2[i]</code> consist of spaces <code>' '</code> and English letters.</li>
|
|
<li>All the strings of <code>list1</code> are <strong>unique</strong>.</li>
|
|
<li>All the strings of <code>list2</code> are <strong>unique</strong>.</li>
|
|
<li>There is at least a common string between <code>list1</code> and <code>list2</code>.</li>
|
|
</ul>
|