BackhardTreesRazorpayZomato

Maximal Bipartite Energy Synthesizer 4 Solution

Problem Statement

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.

Example 1
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.

Example 2
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)
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Maximal Bipartite Energy Synthesizer 4 — Problem Statement & Solution Guide

TreesHardSuffix Automaton
TimeO(n log n)
|
SpaceO(n)

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

Example 1

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.

Example 2

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

JavaScript Solution
Time: O(n log n)
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;
   }

Asked in Top Tech Interviews

RazorpayZomato

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.