Minimum Index Sum of Two Lists

Easy

Description

Given two arrays of strings list1 and list2, find the common strings with the least index sum. Return all such common strings with the minimum index sum. Return 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:

Shogun at indices 0+3 = 3.

Input:list1 = ["Pizza Hut","McDonald's","Subway","Starbucks"], list2 = ["Starbucks","Pizza Hut","Taco Bell","Subway"]
Output:["Pizza Hut"]
Explanation:

Three restaurants appear in both lists: Pizza Hut (0+1=1), Starbucks (3+0=3), and Subway (2+3=5). Pizza Hut has the minimum index sum of 1, so it is the only result.

Input:list1 = ["Apple Store","Best Buy","Target"], list2 = ["Walmart","Target","Best Buy"]
Output:["Best Buy","Target"]
Explanation:

Two stores appear in both lists: Best Buy (1+2=3) and Target (2+1=3). Both have the same minimum index sum of 3, so both are returned in the result.

Constraints

  • 1 ≤ list1.length, list2.length ≤ 1000

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!