Press "Enter" to skip to content

Posts tagged as “pattern”

花花酱 LeetCode 936. Stamping The Sequence

Problem

You want to form aĀ targetĀ string ofĀ lowercase letters.

At the beginning, your sequence isĀ target.lengthĀ '?'Ā marks.Ā  You also have aĀ stampĀ of lowercase letters.

On each turn, you may place the stamp over the sequence, and replace every letter in the sequence with the corresponding letter from the stamp.Ā  You can make up toĀ 10 * target.lengthĀ turns.

For example, if the initial sequence isĀ “?????”, and your stamp isĀ "abc",Ā  then you may makeĀ “abc??”, “?abc?”, “??abc”Ā in the first turn.Ā  (Note that the stamp must be fully contained in the boundaries of the sequence in order to stamp.)

If the sequence is possible to stamp, then return an array ofĀ the index of the left-most letter being stamped at each turn.Ā  If the sequence is not possible to stamp, return an empty array.

For example, if the sequence isĀ “ababc”, and the stamp isĀ "abc", then we could return the answerĀ [0, 2], corresponding to the movesĀ “?????” -> “abc??” -> “ababc”.

Also, if the sequence is possible to stamp, it is guaranteed it is possible to stamp withinĀ 10 * target.lengthĀ moves.Ā  Any answers specifying more than this number of movesĀ will not be accepted.

Example 1:

Input: stamp = "abc", target = "ababc"
Output: [0,2]
([1,0,2] would also be accepted as an answer, as well as some other answers.)

Example 2:

Input: stamp = "abca", target = "aabcaca"
Output: [3,0,1]

Note:

  1. 1 <= stamp.length <= target.length <= 1000
  2. stampĀ andĀ targetĀ only contain lowercase letters.

Solution: Greedy + Reverse Simulation

Reverse the stamping process. Each time find a full or partial match. Replace the matched char to ‘?’.

Don’t forget the reverse the answer as well.

T = “ababc”, S = “abc”

T = “ab???”, index = 2

T = “?????”, index = 0

ans = [0, 2]

Time complexity: O((T – S)*S)

Space complexity: O(T)

C++

花花酱 LeetCode 927. Three Equal Parts

Problem

Given an arrayĀ AĀ ofĀ 0s andĀ 1s, divide the array into 3 non-empty parts such that all of these parts represent the same binary value.

If it is possible, returnĀ anyĀ [i, j]Ā withĀ i+1 < j, such that:

  • A[0], A[1], ..., A[i]Ā is the first part;
  • A[i+1], A[i+2], ..., A[j-1]Ā is the second part, and
  • A[j], A[j+1], ..., A[A.length - 1]Ā is the third part.
  • All three parts have equal binary value.

If it is not possible, returnĀ [-1, -1].

Note that the entire part is used when considering what binary value it represents.Ā  For example,Ā [1,1,0]Ā representsĀ 6Ā in decimal,Ā notĀ 3.Ā  Also, leading zeros are allowed, soĀ [0,1,1]Ā andĀ [1,1]Ā represent the same value.

 

Example 1:

Input: [1,0,1,0,1]
Output: [0,3]

Example 2:

Input: [1,1,0,1,1]
Output: [-1,-1]

Note:

  1. 3 <= A.length <= 30000
  2. A[i] == 0Ā orĀ A[i] == 1

Solution:

each part should have the same number of 1 s.

Find the suffix (without leading os) of the last part which should have 1/3 of the total ones.

Time complexity: O(n^2) in theory but close to O(n) in practice

Space complexity: O(n)

C++