FastPrepShortest Sequence of Source Subsequences

Shortest Sequence of Source Subsequences

Pinterest logoPinterest● MediumFULLTIMEONSITE INTERVIEW
Learn

Problem statement

Form target by concatenating subsequences of source. Return the minimum number of source subsequences required, or -1 when impossible. An empty target requires zero subsequences.

Function

shortestSourceSubsequences(source: String, target: String) → int

Examples

Example 1

source = "abc"target = "abcbc"return = 2

Use abc followed by bc.

Example 2

source = "abc"target = "acdbc"return = -1

The character d never occurs in source.

Example 3

source = "xyz"target = ""return = 0

No subsequence is needed.

Constraints

  • 1 <= source.length <= 10^5.
  • 0 <= target.length <= 10^5.
  • Both strings contain lowercase English letters.

More Pinterest problems

See Pinterest hiring insights
public int shortestSourceSubsequences(String source, String target) {
    // Write your solution here.
}
source"abc"
target"abcbc"
expected2
Checking account…