Count Beautiful Splits in an Array

You are given an array nums.

A split of an array nums is beautiful if:

  • The array nums is split into three subarrays: nums1, nums2, and nums3, such that nums can be formed by concatenating nums1, nums2, and nums3 in that order.
  • The subarray nums1 is a prefix of nums2 OR nums2 is a prefix of nums3.

Return the number of ways you can make this split.

Example 1
Inputnums = [1,1,2,1]
Output2
The beautiful splits are with nums1 = [1], nums2 = [1,2], nums3 = [1], and with nums1 = [1], nums2 = [1], nums3 = [2,1].
Example 2
Inputnums = [1,2,3,4]
Output0
There are 0 beautiful splits.

Constraints

  • 1 <= nums.length <= 5000
  • 0 <= nums[i] <= 50

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate