Longest Common Contiguous User Actions
Problem statement
Two ordered arrays, first and second, contain actions performed by two users. Return their longest common contiguous block as an array of action strings. Consecutive actions in the result must occupy adjacent positions in each original array; you may not skip actions inside a block.
For this exercise, assume actions are complete, nonempty, case-sensitive strings. Compare whole tokens, allow repeated tokens and do not change either input.
For this exercise, assume equal-length answers choose the block that starts earliest in first. Return an empty array when there is no nonempty common block, including when either input array is empty.
Function
longestCommonActions(first: String[], second: String[]) → String[]Examples
Example 1
first = ["hi","bye","hello","leetcode","start","end"]second = ["hi","stop","leetcode","start","end","bye"]return = ["leetcode","start","end"]The original source returns leetcode, start, end: these three actions occupy adjacent positions in both arrays. Adding hi would require skipping actions and is not a contiguous block.
Example 2
first = ["a","b"]second = ["b","a"]return = ["a"]Both one-action blocks have maximum length. Choose a because its block starts earlier in the first array.
Constraints
- For this exercise, assume
0 <= first.length, second.length <= 150. - For this exercise, assume every token has
1to30printable ASCII characters with codes33through126. - Inputs preserve action order; repeated values are valid and no normalization is applied.