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 ", "Burger King ", "KFC "], list2 = [ "Piatti ", "The Grill at Torrey Pines ", "Hungry Hunter Steakhouse ", "Shogun "] Output: [ "Shogun "] Explanation: The only common string is "Shogun ".
Example 2:
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.
Example 3:
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 <= 10001 <= list1[i].length, list2[i].length <= 30list1[i] and list2[i] consist of spaces ' ' and English letters.list1 are unique.list2 are unique.list1 and list2.program main
use, intrinsic :: iso_fortran_env, only : error_unit, output_unit
implicit none
character(len=:), allocatable :: list1(:), list2(:)
integer :: i, j, k, n, m
type(string_set) :: common_strings
call get_command_argument(1, n)
allocate(list1(n))
do i = 1, n
call get_command_argument(i+1, list1(i))
end do
call get_command_argument(2, m)
allocate(list2(m))
do j = 1, m
call get_command_argument(j+1+n, list2(j))
end do
common_strings = get_common_strings(list1, list2)
write (output_unit, '(*(g0,:," ",a))') common_strings%strings
contains
function get_common_strings(list1, list2) result(common_strings)
implicit none
character(len=*), intent(in) :: list1(:), list2(:)
type(string_set) :: common_strings
integer :: i, j
common_strings = string_set()
do i = 1, size(list1)
do j = 1, size(list2)
if (list1(i) == list2(j)) then
call common_strings%add(list1(i))
end if
end do
end do
end function get_common_strings
type :: string_set
class(*), pointer :: strings(:) => null()
contains
procedure :: add => add_string
generic :: assignment(=) => add
end type string_set
subroutine add_string(self, string)
implicit none
class(string_set), intent(inout) :: self
character(len=*), intent(in) :: string
integer :: i
if (.not. associated(self%strings)) then
allocate(self%strings(1))
self%strings(1) = string
else
do i = 1, size(self%strings)
if (self%strings(i) == string) return
end do
allocate(self%strings(size(self%strings)+1))
self%strings(size(self%strings)) = string
end if
end subroutine add_string
end program main
temp.f95:6:21:
6 | type(string_set) :: common_strings
| 1
Error: Derived type âstring_setâ at (1) is being used before it is defined
temp.f95:9:13:
9 | allocate(list1(n))
| 1
Error: Allocate-object at (1) with a deferred type parameter requires either a type-spec or SOURCE tag or a MOLD tag
temp.f95:15:13:
15 | allocate(list2(m))
| 1
Error: Allocate-object at (1) with a deferred type parameter requires either a type-spec or SOURCE tag or a MOLD tag
temp.f95:22:59:
22 | write (output_unit, '(*(g0,:," ",a))') common_strings%strings
| 1
Error: Symbol âcommon_stringsâ at (1) has no IMPLICIT type
temp.f95:29:25:
29 | type(string_set) :: common_strings
| 1
Error: Derived type âstring_setâ at (1) is being used before it is defined
temp.f95:26:68:
26 | function get_common_strings(list1, list2) result(common_strings)
| 1
Error: Function result âcommon_stringsâ at (1) has no IMPLICIT type
temp.f95:37:39:
37 | call common_strings%add(list1(i))
| 1
Error: VARIABLE attribute of âcommon_stringsâ conflicts with PROCEDURE attribute at (1)
temp.f95:44:22:
44 | type :: string_set
| 1
Error: Unexpected derived type declaration statement in CONTAINS section at (1)
temp.f95:45:49:
45 | class(*), pointer :: strings(:) => null()
| 1
Error: Unexpected data declaration statement in CONTAINS section at (1)
temp.f95:46:12:
46 | contains
| 1
Error: Unexpected CONTAINS statement in CONTAINS section at (1)
temp.f95:49:7:
49 | end type string_set
| 1
Error: Expecting END PROGRAM statement at (1)
temp.f95:53:26:
53 | class(string_set), intent(inout) :: self
| 1
Error: Derived type âstring_setâ at (1) is being used before it is defined
temp.f95:57:35:
57 | if (.not. associated(self%strings)) then
| 1
Error: Symbol âselfâ at (1) has no IMPLICIT type
temp.f95:58:27:
58 | allocate(self%strings(1))
| 1
Error: Symbol âselfâ at (1) has no IMPLICIT type
temp.f95:59:18:
59 | self%strings(1) = string
| 1
Error: Symbol âselfâ at (1) has no IMPLICIT type
temp.f95:60:12:
60 | else
| 1
Error: Unexpected ELSE statement at (1)
temp.f95:61:33:
61 | do i = 1, size(self%strings)
| 1
Error: Symbol âselfâ at (1) has no IMPLICIT type
temp.f95:62:26:
62 | if (self%strings(i) == string) return
| 1
Error: Symbol âselfâ at (1) has no IMPLICIT type
temp.f95:63:15:
63 | end do
| 1
Error: Expecting END SUBROUTINE statement at (1)
temp.f95:64:27:
64 | allocate(self%strings(size(self%strings)+1))
| 1
Error: Symbol âselfâ at (1) has no IMPLICIT type
temp.f95:65:18:
65 | self%strings(size(self%strings)) = string
| 1
Error: Symbol âselfâ at (1) has no IMPLICIT type
temp.f95:66:11:
66 | end if
| 1
Error: Expecting END SUBROUTINE statement at (1)
temp.f95:51:30:
51 | subroutine add_string(self, string)
| 1
Error: Symbol âselfâ at (1) has no IMPLICIT type
temp.f95:20:18:
20 | common_strings = get_common_strings(list1, list2)
| 1
Error: Symbol âcommon_stringsâ at (1) has no IMPLICIT type
temp.f95:32:25:
32 | common_strings = string_set()
| 1
Error: Function âstring_setâ at (1) has no IMPLICIT type
temp.f95:8:33:
8 | call get_command_argument(1, n)
| 1
Error: In call to âget_command_argumentâ at (1), type mismatch in argument âvalueâ; pass âINTEGER(4)â to âCHARACTER(*)â
temp.f95:14:33:
14 | call get_command_argument(2, m)
| 1
Error: In call to âget_command_argumentâ at (1), type mismatch in argument âvalueâ; pass âINTEGER(4)â to âCHARACTER(*)â
module common_strings
implicit none
contains
subroutine solve(list1, list2, common_strings)
! This subroutine takes two arrays of strings list1 and list2, and returns
! all the common strings with the least index sum.
! list1: array of strings
! list2: array of strings
! common_strings: array of strings
character(len=*), intent(in) :: list1(:)
character(len=*), intent(in) :: list2(:)
character(len=*), intent(out) :: common_strings(:)
! Local variables
integer :: i, j, k, l, m, n, o
integer :: index_sum
logical :: found
! Initialize the common strings array
common_strings = ""
! Loop through the elements of list1
do i = 1, size(list1)
! Loop through the elements of list2
do j = 1, size(list2)
! Check if the current element of list1 is in list2
if (index(list2(j), list1(i)) /= 0) then
! If it is, add it to the common strings array
common_strings = [common_strings, list1(i)]
! Check if the current element of list2 is in list1
if (index(list1(i), list2(j)) /= 0) then
! If it is, add it to the common strings array
common_strings = [common_strings, list2(j)]
end if
end if
end do
end do
! Sort the common strings array in ascending order
call sort(common_strings)
! Print the common strings
do i = 1, size(common_strings)
write (*,*) common_strings(i)
end do
end subroutine solve
end module common_strings
program main
use common_strings
implicit none
! Test case 1
character(len=*), parameter :: list1(4) = ["Shogun ", "Tapioca Express ", "Burger King ", "KFC "]
character(len=*), parameter :: list2(4) = ["Piatti ", "The Grill at Torrey Pines ", "Hungry Hunter Steakhouse ", "Shogun "]
call solve(list1, list2, common_strings)
! Test case 2
character(len=*), parameter :: list1(4) = ["Shogun ", "Tapioca Express ", "Burger King ", "KFC "]
character(len=*), parameter :: list2(4) = ["KFC ", "Shogun ", "Burger King "]
call solve(list1, list2, common_strings)
! Test case 3
character(len=*), parameter :: list1(3) = ["happy ", "sad ", "good "]
character(len=*), parameter :: list2(3) = ["sad ", "happy ", "good "]
call solve(list1, list2, common_strings)
end program main
temp.f95:73:53:
73 | character(len=*), parameter :: list1(4) = ["Shogun ", "Tapioca Express ", "Burger King ", "KFC "]
| 1
Error: Different CHARACTER lengths (7/16) in array constructor at (1)
temp.f95:74:53:
74 | character(len=*), parameter :: list2(4) = ["Piatti ", "The Grill at Torrey Pines ", "Hungry Hunter Steakhouse ", "Shogun "]
| 1
Error: Different CHARACTER lengths (7/26) in array constructor at (1)
temp.f95:76:40:
76 | call solve(list1, list2, common_strings)
| 1
Error: Symbol at (1) is not appropriate for an expression
temp.f95:79:53:
79 | character(len=*), parameter :: list1(4) = ["Shogun ", "Tapioca Express ", "Burger King ", "KFC "]
| 1
Error: Different CHARACTER lengths (7/16) in array constructor at (1)
temp.f95:80:50:
80 | character(len=*), parameter :: list2(4) = ["KFC ", "Shogun ", "Burger King "]
| 1
Error: Different CHARACTER lengths (4/7) in array constructor at (1)
temp.f95:82:40:
82 | call solve(list1, list2, common_strings)
| 1
Error: Symbol at (1) is not appropriate for an expression
temp.f95:85:52:
85 | character(len=*), parameter :: list1(3) = ["happy ", "sad ", "good "]
| 1
Error: Different CHARACTER lengths (6/4) in array constructor at (1)
temp.f95:86:50:
86 | character(len=*), parameter :: list2(3) = ["sad ", "happy ", "good "]
| 1
Error: Different CHARACTER lengths (4/6) in array constructor at (1)
temp.f95:88:40:
88 | call solve(list1, list2, common_strings)
| 1
Error: Symbol at (1) is not appropriate for an expression
from typing import List
def findRestaurant(list1: List[str], list2: List[str]) -> List[str]:
restaurantMap = {restaurant: i for i, restaurant in enumerate(list1)}
minSum = float("inf")
result = []
for j, restaurant in enumerate(list2):
if restaurant in restaurantMap:
totalSum = j + restaurantMap[restaurant]
if totalSum < minSum:
result = [restaurant]
minSum = totalSum
elif totalSum == minSum:
result.append(restaurant)
return result
1. Create a hashmap to store the restaurants and their indices (or just index for Python and JavaScript) in list1.
#include <vector>
#include <unordered_map>
#include <string>
std::vector<std::string> findRestaurant(std::vector<std::string>& list1, std::vector<std::string>& list2) {
std::unordered_map<std::string, int> restaurantMap;
for (int i = 0; i < list1.size(); i++) {
restaurantMap[list1[i]] = i;
}
int minSum = INT_MAX;
std::vector<std::string> result;
for (int j = 0; j < list2.size(); j++) {
if (restaurantMap.count(list2[j])) {
int sum = j + restaurantMap[list2[j]];
if (sum < minSum) {
result.clear();
result.push_back(list2[j]);
minSum = sum;
} else if (sum == minSum) {
result.push_back(list2[j]);
}
}
}
return result;
}
1. Create a hashmap to store the restaurants and their indices (or just index for Python and JavaScript) in list1.