Advertisement

Minimum Index Sum of Two Lists - LeetCode 599 Solution

Minimum Index Sum of Two Lists - Complete Solution Guide

Minimum Index Sum of Two Lists is LeetCode problem 599, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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 . Example 1: Input: list1 = ["Shogun","Tapioca Express","Burg

Detailed Explanation

The problem requires finding common strings between two lists and returning those that have the minimum sum of their indices in the respective lists. The input consists of two string arrays, `list1` and `list2`. The output should be a list of strings that are present in both input lists, such that the sum of their indices (index in `list1` + index in `list2`) is the smallest among all common strings. We are guaranteed that at least one common string exists.

Solution Approach

The solution involves creating a hash table from one of the lists (typically the first list) that stores each string as a key and its index as the value. Then, it iterates through the second list. For each string in the second list, it checks if the string is present in the hash table. If it is, it calculates the index sum (index in the second list + index from the hash table). If the current index sum is less than the minimum index sum seen so far, it updates the minimum index sum and resets the result list to contain only the current string. If the current index sum is equal to the minimum index sum, it appends the current string to the result list. Finally, it returns the result list.

Step-by-Step Algorithm

  1. Step 1: Create a hash table (dictionary or map) called `dict1` to store strings from `list1` as keys and their corresponding indices as values.
  2. Step 2: Initialize `min_sum` to a large value (e.g., infinity in Python, Integer.MAX_VALUE in Java, INT_MAX in C++) to represent the minimum index sum found so far.
  3. Step 3: Initialize an empty list called `res` to store the common strings with the minimum index sum.
  4. Step 4: Iterate through `list2` with index `i` and string `item`.
  5. Step 5: Check if `item` exists as a key in `dict1` (meaning it's a common string).
  6. Step 6: If `item` is in `dict1`, calculate the current index sum `curr_sum = i + dict1[item]`.
  7. Step 7: If `curr_sum` is less than `min_sum`, update `min_sum` to `curr_sum`, clear the `res` list, and add `item` to `res`.
  8. Step 8: If `curr_sum` is equal to `min_sum`, append `item` to `res`.
  9. Step 9: After iterating through `list2`, return the `res` list.

Key Insights

  • Insight 1: Using a hash table (dictionary or map) to store the strings and their indices from one list allows for efficient lookup in the second list.
  • Insight 2: Iterating through the second list and checking if each string exists in the hash table allows for finding common strings and calculating their index sum.
  • Insight 3: Keeping track of the minimum index sum encountered so far and storing the corresponding strings enables us to identify the common strings with the smallest index sum.

Complexity Analysis

Time Complexity: O(n+m)

Space Complexity: O(n)

Topics

This problem involves: Array, Hash Table, String.

Companies

Asked at: Yelp.