FastPrepCan Subsequences Share One Master Sequence?

Can Subsequences Share One Master Sequence?

Google logoGoogle● MediumNEW GRADONSITE INTERVIEW
Learn

Problem statement

Each row of subsequences lists distinct tokens in the order in which they must appear.

Decide whether there exists one master sequence that contains every distinct token exactly once and has every row as a subsequence.

Return true when such a master sequence exists, otherwise return false. You do not need to construct the master sequence.

Function

canShareMasterSequence(subsequences: String[][]) → boolean

Examples

Example 1

subsequences = [["a","b"],["a","c"]]return = true

Both rows are subsequences of [a,b,c].

Example 2

subsequences = [["a","b"],["b","a"]]return = false

The first row requires a before b, while the second requires the reverse.

Example 3

subsequences = [["a","c"],["a","b","c"],["b","d"]]return = true

[a,b,c,d] respects every reported order constraint.

Constraints

  • 1 <= subsequences.length <= 100000.
  • Every row contains from 1 through 100000 distinct tokens.
  • The total number of token occurrences is at most 200000.
  • Every token is a non-empty lowercase English string of length at most 30.

More Google problems

See Google hiring insights
public boolean canShareMasterSequence(String[][] subsequences) {
    // write your code here
}
subsequences[["a","b"],["a","c"]]
expectedtrue
Checking account…