Problem · String

Get Patch Sequence

Learn this problem
HardMorgan StanleyOA

Problem statement

A clothing company uses a reusable string patch to create designerWords. Begin with designerWords.length blank positions. Applying the patch at zero-based index i writes every character of patch into positions i through i + patch.length - 1. Written characters overwrite anything already present at those positions.

A sequence is valid when all positions spell designerWords after the final application. Each starting index may appear at most once.

Return a valid sequence using the fewest patch applications. If several minimum-length sequences exist, return the lexicographically smallest one: compare the first differing index, and if one sequence is a prefix of another, the shorter sequence is smaller. Return [-1] when no valid sequence exists.

Function

getPatchSequence(patch: String, designerWords: String) → int[]

Examples

Example 1

patch = "ab"designerWords = "aab"return = [0, 1]
The patch needs to be used in the following way to create the desired word: Place the first patch at index 0 to get "ab". Place the second patch at index 1 to form "aab". The requirement states that the second patch 'b' of the first patch overlaps with 'a' of the second patch.

Example 2

patch = "yz"designerWords = "yzzz"return = [2, 1, 0]
:)

Constraints

  • 1 ≤ patch.length ≤ designerWords.length ≤ 20
  • patch and designerWords contain only lowercase English letters.
  • Each valid starting index is in the inclusive range 0 through designerWords.length - patch.length.
  • Each starting index may appear at most once.

More Morgan Stanley problems

drafts saved locally
public int[] getPatchSequence(String patch, String designerWords) {
  // write your code here
}
patch"ab"
designerWords"aab"
expected[0, 1]
checking account