3388. Count Beautiful Splits in an Array LeetCode Solution
In this guide, you will get 3388. Count Beautiful Splits in an Array LeetCode Solution with the best time and space complexity. The solution to Count Beautiful Splits in an Array problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Count Beautiful Splits in an Array solution in C++
- Count Beautiful Splits in an Array solution in Java
- Count Beautiful Splits in an Array solution in Python
- Additional Resources
Problem Statement of 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:
Input: nums = [1,1,2,1]
Output: 2
Explanation:
The beautiful splits are:
A split with nums1 = [1], nums2 = [1,2], nums3 = [1].
A split with nums1 = [1], nums2 = [1], nums3 = [2,1].
Example 2:
Input: nums = [1,2,3,4]
Output: 0
Explanation:
There are 0 beautiful splits.
Constraints:
1 <= nums.length <= 5000
0 <= nums[i] <= 50
Complexity Analysis
- Time Complexity: O(n^2)
- Space Complexity: O(n^2)
3388. Count Beautiful Splits in an Array LeetCode Solution in C++
class Solution {
public:
int beautifulSplits(vector<int>& nums) {
const int n = nums.size();
int ans = 0;
// z[start][i] := the z array of nums[i..n) with respect to nums[start..n)
vector<vector<int>> z;
for (int start = 0; start < n; ++start)
z.push_back(zFunction(nums, start));
// nums1 | nums2 | nums3 = nums[0..i] | nums[i + 1..j] | nums[j + 1..n - 1]
for (int i = 0; i < n - 2; ++i)
for (int j = i + 1; j < n - 1; ++j)
if ((j - i >= i + 1 &&
z[0][i + 1] >= i + 1) || // nums1 is a prefix of nums2.
z[i + 1][j + 1] >= j - i) // nums2 is a suffix of nums3.
++ans;
return ans;
}
private:
// Returns the z array, where z[i] is the length of the longest prefix of
// nums[i..n) which is also a prefix of nums[start..n).
vector<int> zFunction(const vector<int>& nums, int start) {
const int n = nums.size();
vector<int> z(n);
int l = 0;
int r = 0;
for (int i = 1 + start; i < n; ++i) {
if (i < r)
z[i] = min(r - i, z[i - l + start]);
while (i + z[i] < n && nums[z[i] + start] == nums[i + z[i]])
++z[i];
if (i + z[i] > r) {
l = i;
r = i + z[i];
}
}
return z;
}
};
/* code provided by PROGIEZ */
3388. Count Beautiful Splits in an Array LeetCode Solution in Java
class Solution {
public int beautifulSplits(int[] nums) {
final int n = nums.length;
int ans = 0;
// z[start][i] := the z array of nums[i..n) with respect to nums[start..n)
int[][] z = new int[n][];
for (int start = 0; start < n; ++start)
z[start] = zFunction(nums, start);
// nums1 | nums2 | nums3 = nums[0..i] | nums[i + 1..j] | nums[j + 1..n - 1]
for (int i = 0; i < n - 2; ++i)
for (int j = i + 1; j < n - 1; ++j)
if ((j - i >= i + 1 && z[0][i + 1] >= i + 1) || // nums1 is a prefix of nums2.
z[i + 1][j + 1] >= j - i) // nums2 is a suffix of nums3.
++ans;
return ans;
}
// Returns the z array, where z[i] is the length of the longest prefix of
// nums[i..n) which is also a prefix of nums[start..n).
private int[] zFunction(int[] nums, int start) {
final int n = nums.length;
int[] z = new int[n];
int l = 0;
int r = 0;
for (int i = 1 + start; i < n; ++i) {
if (i < r)
z[i] = Math.min(r - i, z[i - l + start]);
while (i + z[i] < n && nums[z[i] + start] == nums[i + z[i]])
++z[i];
if (i + z[i] > r) {
l = i;
r = i + z[i];
}
}
return z;
}
}
// code provided by PROGIEZ
3388. Count Beautiful Splits in an Array LeetCode Solution in Python
class Solution:
def beautifulSplits(self, nums: list[int]) -> int:
n = len(nums)
# z[start][i] := the z array of nums[i..n) with respect to nums[start..n)
z = [self._zFunction(nums, start)
for start in range(n)]
# nums1 | nums2 | nums3 = nums[0..i] | nums[i + 1..j] | nums[j + 1..n - 1]
return sum((j - i >= i + 1 and z[0][i + 1] >= i + 1) # nums1 is a prefix of nums2
or z[i + 1][j + 1] >= j - i # nums2 is a suffix of nums3.
for i in range(n - 2)
for j in range(i + 1, n - 1))
def _zFunction(self, nums: list[int], start: int) -> list[int]:
"""
Returns the z array, where z[i] is the length of the longest prefix of
nums[i..n) which is also a prefix of nums[start..n).
"""
n = len(nums)
z = [0] * n
l = start
r = start
for i in range(1 + start, n):
if i < r:
z[i] = min(r - i, z[i - l + start])
while i + z[i] < n and nums[z[i] + start] == nums[i + z[i]]:
z[i] += 1
if i + z[i] > r:
l = i
r = i + z[i]
return z
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.