> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Minimum Index Sum of Two Lists Python Solution

> Tested Python solution for LeetCode 599 with 18 pytest cases. Generate a practice environment with lcpy.

LeetCode 599, [Easy](/catalog/easy). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [String](/catalog/topics/string). [View on LeetCode](https://leetcode.com/problems/minimum-index-sum-of-two-lists/description/).

Generate this problem as a practice environment: tested reference solution, 18 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 599   # by problem number
lcpy gen -s minimum_index_sum_of_two_lists   # by problem name
```

## Problem

Given two arrays of strings `list1` and `list2`, find the **common strings with the least index sum**.

A **common string** is a string that appeared in both `list1` and `list2`.

A **common string with the least index sum** is a common string such that if it appeared at `list1[i]` and `list2[j]` then `i + j` should be the minimum value among all the other **common strings**.

Return *all the **common strings with the least index sum***. Return the answer in **any order**.

### Examples

```
Input: list1 = ["Shogun","Tapioca Express","Burger King","KFC"], list2 = ["Piatti","The Grill at Torrey Pines","Hungry Hunter Steakhouse","Shogun"]
Output: ["Shogun"]
Explanation: The only common string is "Shogun".
```

```
Input: list1 = ["Shogun","Tapioca Express","Burger King","KFC"], list2 = ["KFC","Shogun","Burger King"]
Output: ["Shogun"]
Explanation: The common string with the least index sum is "Shogun" with index sum = (0 + 1) = 1.
```

```
Input: list1 = ["happy","sad","good"], list2 = ["sad","happy","good"]
Output: ["sad","happy"]
Explanation: 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".
```

### Constraints

* 1 \<= list1.length, list2.length \<= 1000
* 1 \<= list1\[i].length, list2\[i].length \<= 30
* list1\[i] and list2\[i] consist of spaces ' ' and English letters.
* All the strings of list1 are unique.
* All the strings of list2 are unique.
* There is at least a common string between list1 and list2.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/minimum_index_sum_of_two_lists/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/minimum_index_sum_of_two_lists/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n + m)
    # Space: O(m)
    def find_restaurant(self, list1: list[str], list2: list[str]) -> list[str]:
        index_in_list2 = {s: i for i, s in enumerate(list2)}
        best_sum = len(list1) + len(list2)
        result: list[str] = []
        for i, s in enumerate(list1):
            j = index_in_list2.get(s)
            if j is None:
                continue
            total = i + j
            if total < best_sum:
                best_sum = total
                result = [s]
            elif total == best_sum:
                result.append(s)
        return result
```

## Complexity

| Time | Space |
| - | - |
| O(n + m) | O(m) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.