Longest Common Prefix of K Strings After Removal - Problem

You are given an array of strings words and an integer k. Your task is to analyze what happens when each string is temporarily removed from the array.

Goal: For each index i, find the length of the longest common prefix among any k strings from the remaining array (after removing the string at index i).

Key Points:

  • You must select exactly k strings from distinct indices
  • Find the longest prefix that is common to all k selected strings
  • If removing element i leaves fewer than k strings, return 0
  • Return an array where answer[i] represents the answer for removing the i-th element

Example: If words = ["abc", "abcd", "ab", "a"] and k = 2, after removing index 0 ("abc"), we have ["abcd", "ab", "a"]. The best pair might be "abcd" and "ab" with common prefix "ab" (length 2).

Input & Output

Basic Example
$ Input: words = ["abc", "abcd", "ab", "a"], k = 2
โ€บ Output: [2, 2, 1, 2]
๐Ÿ’ก Note: Remove 'abc': remaining ['abcd','ab','a'], best pair 'abcd'+'ab' = prefix 'ab' (length 2). Remove 'abcd': remaining ['abc','ab','a'], best pair 'abc'+'ab' = prefix 'ab' (length 2). Remove 'ab': remaining ['abc','abcd','a'], best pair 'abc'+'abcd' = prefix 'a' (length 1). Remove 'a': remaining ['abc','abcd','ab'], best pair 'abc'+'abcd' = prefix 'abc' (length 2).
Insufficient Strings
$ Input: words = ["hello", "world"], k = 3
โ€บ Output: [0, 0]
๐Ÿ’ก Note: After removing any element, we have only 1 string remaining, which is less than k=3. Therefore, answer is 0 for all indices.
No Common Prefix
$ Input: words = ["xyz", "abc", "def"], k = 2
โ€บ Output: [0, 0, 0]
๐Ÿ’ก Note: No matter which element we remove, the remaining strings have no common prefixes. For example, removing 'xyz' leaves ['abc', 'def'] with no common prefix.

Visualization

Tap to expand
Removed ShelfRemaining BooksROOTABCBCEach node countsbooks with that prefixFind deepest levelwith โ‰ฅ k books
Understanding the Visualization
1
Remove Shelf
Take away one shelf of books (remove string at index i)
2
Build Prefix Tree
Organize remaining books by their catalog prefixes in a tree structure
3
Count Books
At each tree level, count how many books share that prefix
4
Find Deepest Level
Locate the deepest tree level that still has at least k books
Key Takeaway
๐ŸŽฏ Key Insight: A Trie naturally organizes strings by prefixes, allowing us to efficiently find the longest common prefix by traversing to the deepest level with sufficient string count.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n * m * |remaining|)

For each of n removals, build a Trie taking O(m * |remaining|) where m is average string length and |remaining| is number of remaining strings

n
2n
โœ“ Linear Growth
Space Complexity
O(m * |remaining|)

Trie storage requires space proportional to total characters in all strings

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค words.length โ‰ค 100
  • 1 โ‰ค words[i].length โ‰ค 100
  • 1 โ‰ค k โ‰ค words.length
  • words[i] consists of lowercase English letters only
  • All strings in words are distinct
Asked in
Google 35 Amazon 28 Meta 22 Microsoft 18
22.2K Views
Medium Frequency
~25 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen