FastPrepLongest Common Contiguous User Actions

Longest Common Contiguous User Actions

Wayfair logoWayfair● MediumFULLTIMEPHONE SCREEN
Learn

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 1 to 30 printable ASCII characters with codes 33 through 126.
  • Inputs preserve action order; repeated values are valid and no normalization is applied.

More Wayfair problems

See Wayfair hiring insights
public String[] longestCommonActions(String[] first, String[] second) {
    // Write your code here.
    return new String[0];
}
first["hi","bye","hello","leetcode","start","end"]
second["hi","stop","leetcode","start","end","bye"]
expected["leetcode", "start", "end"]
Checking account…