Ctrl K

Three Pieces With a Shared Start

Medium
You are given an integer array seq. Cut it into three non-empty consecutive pieces A, B and C, so that A + B + C == seq. A cut is good if at least one of these holds: - A is a prefix of B; - B is a prefix of C. A piece counts as a prefix of itself. Return the number of good cuts.
Example 1
seq=[5, 1, 5, 1, 5] → 2

Two cuts work: [5, 1] | [5, 1] | [5], where the first piece starts the second, and [5] | [1, 5] | [1, 5], where the second piece starts the third.

Example 2
seq=[3, 3, 3] → 1

There is only one cut, [3] | [3] | [3], and it works.

Example 3
seq=[4, 7, 9] → 0

The only cut, [4] | [7] | [9], fails: all pieces differ.

Constraints
  • 1 ≤ len(seq) ≤ 5000
  • 0 ≤ seq[i] ≤ 50
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsFewest Jumps to the End MediumMost Profitable Streak MediumReach the Last Cell MediumWarehouse Cart Routes MediumCheapest Corner-to-Corner Route Medium
loading Python…
Input
seq=[5, 1, 5, 1, 5]
Expected
2

Run checks the first 3 tests in your browser. Submit checks all 109 tests on the server, hidden ones included.