Class Solution
java.lang.Object
g3501_3600.s3598_longest_common_prefix_between_adjacent_strings_after_removals.Solution
3598 - Longest Common Prefix Between Adjacent Strings After Removals.
Medium
You are given an array of strings words. For each index i in the range [0, words.length - 1], perform the following steps:
- Remove the element at index
ifrom thewordsarray. - Compute the length of the longest common prefix among all adjacent pairs in the modified array.
Return an array answer, where answer[i] is the length of the longest common prefix between the adjacent pairs after removing the element at index i. If no adjacent pairs remain or if none share a common prefix, then answer[i] should be 0.
Example 1:
Input: words = [“jump”,“run”,“run”,“jump”,“run”]
Output: [3,0,0,3,3]
Explanation:
- Removing index 0:
wordsbecomes["run", "run", "jump", "run"]- Longest adjacent pair is
["run", "run"]having a common prefix"run"(length 3)
- Removing index 1:
wordsbecomes["jump", "run", "jump", "run"]- No adjacent pairs share a common prefix (length 0)
- Removing index 2:
wordsbecomes["jump", "run", "jump", "run"]- No adjacent pairs share a common prefix (length 0)
- Removing index 3:
wordsbecomes["jump", "run", "run", "run"]- Longest adjacent pair is
["run", "run"]having a common prefix"run"(length 3)
- Removing index 4:
- words becomes
["jump", "run", "run", "jump"] - Longest adjacent pair is
["run", "run"]having a common prefix"run"(length 3)
- words becomes
Example 2:
Input: words = [“dog”,“racer”,“car”]
Output: [0,0,0]
Explanation:
- Removing any index results in an answer of 0.
Constraints:
1 <= words.length <= 1051 <= words[i].length <= 104words[i]consists of lowercase English letters.- The sum of
words[i].lengthis smaller than or equal105.
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
longestCommonPrefix
-