CSES Solutions - Substring Distribution
Last Updated :
23 Jul, 2025
You are given a string of length N. For every integer between 1 to N you need to print the number of distinct substrings of that length.
Prerequisites to Solve this problem: Suffix arrays, LCP
Examples:
Input: str="abab"
Output: 2 2 2 1
Explanation:
- for length n=1, distinct substrings: {a, b}
- for length n=2, distinct substrings: {ab, ba}
- for length n=3, distinct substrings: {aba, bab}
- for length n=4, distinct substrings: {abab}
Input: str="abc"
Output: 3 2 1
Explanation:
- for length n=1, distinct substrings: {a, b, c}
- for length n=2, distinct substrings: {ab, bc}
Approach using Suffix array and Longest Common Prefix(LCP):
Suffix Arrays: A suffix array is a sorted array of all suffixes of a given string. The suffixes are usually represented as integers, which are the starting indices of the suffixes in the string.
Longest Common Prefix (LCP) Array: The LCP array is an auxiliary data structure that stores the length of the longest common prefix between any two consecutive suffixes in the sorted suffix array.
The below image shows how Suffix array and LCP array look for a string "BANANA"

Key Observations:
- For string of length N, the contribution of unique substrings for given suffix at index i is Suffix[i] = N - Suffix[i] - LCP[i]
- Proof:
N - Suffix[i]
represents the total number of substrings that do not start from index i
(including substrings that start from previous suffixes).- Now,
LCP[i]
represents the number of substrings shared with the next suffix. Subtracting LCP[i]
from N - Suffix[i]
removes the common substrings shared with the next suffix, leaving only the unique substrings contributed by the suffix starting at index i
- Therefore,
Suffix[i] = N - Suffix[i] - LCP[i]
represents the number of unique substrings contributed by the suffix starting at index i
.
- This means that for a given suffix, it will contain unique substrings of lengths from LCP[i]+1 to N - Suffix[i].
So, for each suffix in the suffix array, you can calculate the range of lengths of unique substrings it contributes, and update the count of substrings of those lengths accordingly.
Building Suffix Array: The buildSuffixArray function is used to build the suffix array for the input string. It starts by initializing the suffix array and the position array. Then it sorts the suffix array based on the characters at the current gap. The gap is initially 1 and is doubled in each iteration. The position array is updated in each iteration based on the sorted suffix array.
Building LCP Array: The buildLCPArray function is used to build the LCP array for the input string and its suffix array. It iterates over the suffix array and calculates the longest common prefix length for each pair of consecutive suffixes in the sorted suffix array.
Counting Distinct Substrings: After building the suffix array and the LCP array, the next step is to count the number of distinct substrings of each length. This is done by iterating over the LCP array and updating the prefix count array. For each index i in the LCP array, the number of distinct substrings of length greater than LCP[i] and less than or equal to the length of the suffix denoted by suffixArray[i] is incremented. The number of distinct substrings of length greater than the length of the suffix denoted by suffixArray[i] is decremented.
Step-by-Step algorithm:
- Take MAX_LENGTH as the maximum length of the string, suffixArray, position, temp, and longestCommonPrefix are arrays used in the algorithm, gap and stringLength are integers used to store the gap for comparison and the length of the string respectively, and str is the string for which the suffix array and LCP array are to be built.
- Comparator Function: The compare function is used to sort the suffix array. It compares two suffixes of the string based on their positions and the gap.
- Build Suffix Array: The buildSuffixArray function builds the suffix array for the string. It initializes the suffix array and position array, and then sorts the suffix array using the compare function. The position array is updated after each sort.
- Build LCP Array: The buildLCPArray function builds the Longest Common Prefix (LCP) array for the string. It calculates the longest common prefix length for each pair of consecutive suffixes in the sorted suffix array.
- Call the buildSuffixArray and buildLCPArray functions to build the suffix array and LCP array, and then calculates the prefix count for each prefix length. The prefix count is then printed.
Implementation of above approach
C++
#include <bits/stdc++.h>
using namespace std;
// Define constants and global variables
const int MAX_LENGTH
= 1e5 + 5; // Maximum length of the input string
int suffixArray[MAX_LENGTH], position[MAX_LENGTH],
temp[MAX_LENGTH], longestCommonPrefix[MAX_LENGTH];
int gap, stringLength; // Variables for building suffix
// array and LCP array
string str; // Input string
// Comparator function for sorting suffix array
bool compare(int x, int y)
{
// Compare the positions of suffixes
if (position[x] != position[y])
return position[x] < position[y];
x += gap;
y += gap;
// If both suffixes are within the string length,
// compare their positions
return (x < stringLength && y < stringLength)
? position[x] < position[y]
: x > y;
}
// Function to build the suffix array
void buildSuffixArray()
{
// Initialize suffix array and position array
for (int i = 0; i < stringLength; i++)
suffixArray[i] = i, position[i] = str[i];
// Build the suffix array using doubling technique
for (gap = 1;; gap <<= 1) {
// Sort the suffix array based on the current gap
sort(suffixArray, suffixArray + stringLength,
compare);
// Update temporary array for position calculation
for (int i = 0; i < stringLength - 1; i++)
temp[i + 1] = temp[i]
+ compare(suffixArray[i],
suffixArray[i + 1]);
// Update position array
for (int i = 0; i < stringLength; i++)
position[suffixArray[i]] = temp[i];
// Check if all suffixes are in lexicographically
// sorted order
if (temp[stringLength - 1] == stringLength - 1)
break;
}
}
// Function to build the LCP array
void buildLCPArray()
{
for (int i = 0, k = 0; i < stringLength; i++) {
if (position[i] != stringLength - 1) {
int j = suffixArray[position[i] + 1];
// Compute the length of common prefix between
// suffixes
while (str[i + k] == str[j + k])
k++;
longestCommonPrefix[position[i]] = k;
if (k)
k--; // Decrement k if it is non-zero
}
}
}
int prefixCount[MAX_LENGTH]; // Array to store cumulative
// count of distinct substrings
// Driver code
int main()
{
// Input string (change this to your desired input)
str = "abab";
// Compute suffix array and LCP array
stringLength = str.size();
buildSuffixArray();
buildLCPArray();
// Calculate the number of distinct substrings
int previous = 0;
for (int i = 0; i < stringLength; i++) {
prefixCount[previous + 1]++;
prefixCount[stringLength - suffixArray[i] + 1]--;
previous = longestCommonPrefix[i];
}
// Compute the cumulative count of distinct substrings
for (int i = 1; i <= stringLength; i++) {
cout << prefixCount[i] << ' ';
prefixCount[i + 1] += prefixCount[i];
}
return 0;
}
Java
import java.util.HashSet;
import java.util.Set;
public class Main {
static Set<String> distinctSubstrings(String s) {
int n = s.length();
Set<String> substrings = new HashSet<>();
for (int i = 0; i < n; i++) {
for (int j = i + 1; j <= n; j++) {
substrings.add(s.substring(i, j));
}
}
return substrings;
}
static int[] countDistinctSubstrings(String s) {
Set<String> substrings = distinctSubstrings(s);
int[] counts = new int[s.length()];
for (String substring : substrings) {
counts[substring.length() - 1]++;
}
return counts;
}
public static void main(String[] args) {
String s = "abab";
int[] counts = countDistinctSubstrings(s);
for (int count : counts) {
System.out.print(count + " ");
}
}
}
Python3
def distinct_substrings(s):
n = len(s)
substrings = set() # Initialize an empty set to store the substrings
for i in range(n):
for j in range(i+1, n+1):
substrings.add(s[i:j]) # Add each substring to the set
return substrings # Return the set of substrings
def count_distinct_substrings(s):
substrings = distinct_substrings(s) # Get all distinct substrings
counts = [0] * len(s)
# Count the number of substrings of each length
for substring in substrings:
counts[len(substring)-1] += 1
return counts # Return the counts
def main():
s = "abab" # Define the string
counts = count_distinct_substrings(s)
print(*counts) # Print the counts
if __name__ == "__main__":
main()
JavaScript
function distinctSubstrings(s) {
const n = s.length;
const substrings = new Set();
for (let i = 0; i < n; i++) {
for (let j = i + 1; j <= n; j++) {
substrings.add(s.substring(i, j));
}
}
return substrings;
}
function countDistinctSubstrings(s) {
const substrings = distinctSubstrings(s);
const counts = new Array(s.length).fill(0);
substrings.forEach(substring => {
counts[substring.length - 1]++;
});
return counts;
}
function main() {
const s = "abab";
const counts = countDistinctSubstrings(s);
console.log(counts.join(" "));
}
// Calling the main function
main();
Time Complexity: O(nlogn)
Auxiliary Space: O(n)
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem