Find a String Containing Another
Company: Meta
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates proficiency in string algorithms, substring search techniques, and algorithmic complexity analysis within the Coding & Algorithms domain.
Constraints
- 1 <= len(strings) <= 10^4
- 1 <= len(strings[i]) <= 10^3
- The sum of all string lengths is at most 2 * 10^5
- Strings are case-sensitive and duplicate values may appear
Examples
Input: (["programming", "am", "pro"],)
Expected Output: "programming"
Explanation: "programming" contains both "am" and "pro", and no other string in the list contains a different string.
Input: (["cat", "dog", "bird"],)
Expected Output: ""
Explanation: No string contains any other distinct string as a contiguous substring.
Input: (["hello"],)
Expected Output: ""
Explanation: With only one string, there is no different string to be contained.
Input: (["abc", "abc", "x"],)
Expected Output: ""
Explanation: Duplicate copies of "abc" do not count as a distinct contained string, and neither string contains a different value from the list.
Input: (["a", "aa"],)
Expected Output: "aa"
Explanation: "aa" contains "a" as a substring.
Input: (["table", "able", "zzzz"],)
Expected Output: "table"
Explanation: "table" contains "able" as a suffix, so it is a valid answer.
Hints
- Checking every pair of strings separately repeats a lot of work. Can you preprocess all words into one shared search structure?
- A trie with failure links (Aho-Corasick) lets you scan each word once while simultaneously checking for every other word as a substring.