Skip to content

Which are in?

Given two arrays of strings a1 and a2 return a sorted array r in lexicographical order of the strings of a1 which are substrings of strings of a2.

Example

`a1 = ["arp", "live", "strong"]`
`a2 = ["lively", "alive", "harp", "sharp", "armstrong"]`
returns `["arp", "live", "strong"]`

`a1 = ["tarp", "mice", "bull"]`
`a2 = ["lively", "alive", "harp", "sharp", "armstrong"]`
returns `[]`

Note

Beware: r must be without duplicates.

Solution

py
def which_are_in(array1, array2):
    array3 = []
    for i in array1:
        for j in array2:
            if j.find(i) != -1:
                array3.append(i)
                break
    return sorted(list(set(array3)))


print(
    which_are_in(
        ["arp", "live", "strong"],
        ["lively", "alive", "harp", "sharp", "armstrong"]
    )
)

my thoughts are neither my employer's nor my wife's