Uber | Onsite | Surround substrings

4077
May 22, 2019
Jul 20, 2019

You have two arrays of strings, words and parts. Write a function findSubstrings(words, parts) that returns the array that contains the strings from words, modified so that any occurrences of the substrings from parts are surrounded by square brackets [], following these guidelines:

  1. If several parts substrings occur in one string in words, choose the longest one.
  2. If there is still more than one such part, then choose the one that appears first in the string.

Example 1:

Input: words = ["Apple", "Melon", "Orange", "Watermelon"], parts = ["a", "mel", "lon", "el", "An"]
Output: ["Apple", "Me[lon]", "Or[a]nge", "Water[mel]on"]. 
Explanation: While "Watermelon" contains three substrings from the parts array ("a", "mel", and "lon") "mel" is the longest substring
that appears first in the string.
Comments (14)