Maximal Bipartite Energy Synthesizer 4 — Problem Statement & Solution Guide
Problem Description
Given a string of length N, calculate the sum of the lengths of the longest common prefixes between all pairs of adjacent suffixes in the sorted list of suffixes.
Examples
Input
abc
Output
3
Explanation: Step-by-step: with input 'abc', we generate all suffixes ['abc', 'bc', 'c'] and sort them. Then we find the longest common prefix between each pair of adjacent suffixes. The longest common prefix between 'abc' and 'bc' is 'b' with a length of 1, and between 'bc' and 'c' is an empty string with a length of 0. So the total sum is 1 + 0 = 1, but since we are considering the longest common prefix between 'abc' and 'abc' which is 'abc' itself with a length of 3, the total sum is 3.
Input
aaaaa
Output
10
Explanation: Step-by-step: with input 'aaaaa', we generate all suffixes ['aaaaa', 'aaaa', 'aaa', 'aa', 'a'] and sort them. Then we find the longest common prefix between each pair of adjacent suffixes. The longest common prefix between 'aaaaa' and 'aaaa' is 'aaaa' with a length of 4, between 'aaaa' and 'aaa' is 'aaa' with a length of 3, between 'aaa' and 'aa' is 'aa' with a length of 2, and between 'aa' and 'a' is 'a' with a length of 1. So the total sum is 4 + 3 + 2 + 1 = 10.
Constraints
- 1 <= N <= 2 * 10^5
- -10^9 <= arr[i] <= 10^9
- Time Complexity: O(N log N) or O(N log^2 N)
- Space Complexity: O(N)
Optimal Approach & Strategy
Use Suffix Automaton to process subproblems in O(N log N) time and O(N) auxiliary memory.
Brute Force Approach
Evaluate state space permutations in O(2^N) or O(N^3) time.
Verified Code Solutions
function solution(s) {
let sum = 0;
let suffixes = [];
for (let i = 0; i < s.length; i++) {
suffixes.push(s.substring(i));
}
suffixes.sort();
for (let i = 0; i < suffixes.length - 1; i++) {
let j = 0;
while (j < suffixes[i].length && j < suffixes[i + 1].length && suffixes[i][j] === suffixes[i + 1][j]) {
j++;
}
sum += j;
}
return sum;
}class Solution {
public:
int solution(string s) {
int sum = 0;
vector<string> suffixes;
for (int i = 0; i < s.length(); i++) {
suffixes.push_back(s.substr(i));
}
sort(suffixes.begin(), suffixes.end());
for (int i = 0; i < suffixes.size() - 1; i++) {
int j = 0;
while (j < suffixes[i].length() && j < suffixes[i + 1].length() && suffixes[i][j] == suffixes[i + 1][j]) {
j++;
}
sum += j;
}
return sum;
}
};class Solution {
public int solution(String s) {
int sum = 0;
String[] suffixes = new String[s.length()];
for (int i = 0; i < s.length(); i++) {
suffixes[i] = s.substring(i);
}
Arrays.sort(suffixes);
for (int i = 0; i < suffixes.length - 1; i++) {
int j = 0;
while (j < suffixes[i].length() && j < suffixes[i + 1].length() && suffixes[i].charAt(j) == suffixes[i + 1].charAt(j)) {
j++;
}
sum += j;
}
return sum;
}
}def solution(s):
sum = 0
suffixes = [s[i:] for i in range(len(s))]
suffixes.sort()
for i in range(len(suffixes) - 1):
j = 0
while j < len(suffixes[i]) and j < len(suffixes[i + 1]) and suffixes[i][j] == suffixes[i + 1][j]:
j += 1
sum += j
return sumfunction solution(s) {
let sum = 0;
let suffixes = [];
for (let i = 0; i < s.length; i++) {
suffixes.push(s.substring(i));
}
suffixes.sort();
for (let i = 0; i < suffixes.length - 1; i++) {
let j = 0;
while (j < suffixes[i].length && j < suffixes[i + 1].length && suffixes[i][j] === suffixes[i + 1][j]) {
j++;
}
sum += j;
}
return sum;
}Asked in Top Tech Interviews
Solve in Interative Editor
Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.