Finding unique string from two sorted reverse lexical order arrays in Java

Following is the question which I need the best answer for (best time complexity), along with a couple of test cases for it:

Given two arrays of strings a1 and a2, return a sorted array r in reverse lexical order that contains strings from a1 which are substrings of strings in a2. r should also contain unique strings without duplicates.

Example 1:

a1 = ["arp", "live", "strong"]

a2 = ["lively", "alive", "harp", "sharp", "armstrong"]

r = ["strong","live","arp"]

Example 2:

a1 = ["tarp", "mice", "bull"]

a2 = ["lively", "alive", "harp", "sharp", "armstrong"]

r = []

For Custom Orders. Send message on WhatsApp: +92 324 7042178
Powered by