Given an integer n, the task is to count Dyck words. A Dyck word is a string consisting of n X's and n Y's such that no initial segment (prefix) of the string has more Y's than X's.
Examples:
Input: n = 2
Output: 2
Explanation: "XYXY" and "XXYY" are the only possible DYCK words of length 2.Input: n = 5
Output: 42
The following are the Dyck words up to n <= 3:

Proof:
When examining this problem from a geometrical perspective, we can observe and draw conclusions based on the structure of Dyck paths. Below is a visual representation for n = 3.
For a given n, there are 5 possible Dyck words of length 2n. Each Dyck word can be represented as a path on a coordinate grid. The key condition for a Dyck word is that for every prefix of the word, the count of x's (or "up" steps) must be greater than or equal to the count of y's (or "down" steps). This condition can be represented geometrically as a line graph.
Since each Dyck word contains n x's and n y's, we can trace a path from the origin (0, 0) to the point (n, n), where a point (a, b) represents the occurrence of a x's and b y's encountered along the path.
We draw a diagonal line from (0, 0) to (n, n) as a reference, which represents the ideal balance between x's and y's. The condition f(x) >= f(y) ensures that our path never dips Above this diagonal line, staying on or Below it throughout the journey from (0, 0) to (n, n). The graph is formed by dyck path is symmetric. You can visualize this by taking path on above diagonal or below digonal but you can not draw line on both size at one time. Here is the example where all graph is formed by taking path on below line.
The structure of a Dyck path, therefore, allows us to select combinations of points along this journey that satisfy this condition, creating valid paths corresponding to each Dyck word.
We can interpret this theory by observing below images.
You have seen that the number of Dyck paths of length 2n is (1 / n+1) * ( 2*nCn). For more information of Dyck paths you can refer this article : Dyck path
Approach:
This is a classic application of Catalan number. We just need to find nth catalan number. First few catalan numbers are 1 1 2 5 14 42 132 429 1430 4862, … (considered from 0th number)
- Formula of catalan number is (1 / n+1) * ( 2*nCn).
Here you can read more about catala number and it's applications : Applications of Catalan Numbers
// C++ code finds the number of Dyck Words
// of given length
#include <iostream>
using namespace std;
// Function to calculate the binomial coefficient C(n, k)
int binomialCoeff(int n, int k) {
// C(n, k) is the same as C(n, n-k)
if (k > n - k) {
k = n - k;
}
int res = 1;
// Calculate the value of n! / (k! * (n-k)!)
for (int i = 0; i < k; ++i) {
res *= (n - i);
res /= (i + 1);
}
return res;
}
// Function to find the nth Catalan number
int catalan(int n) {
// Calculate C(2n, n)
int c = binomialCoeff(2 * n, n);
// Return the nth Catalan number
return c / (n + 1);
}
int main() {
int n = 3;
cout << catalan(n) << endl;
return 0;
}
// Java code finds the number of Dyck
// Words of given length
class GfG {
// Function to calculate the binomial coefficient C(n,
// k)
static int binomialCoeff(int n, int k) {
// C(n, k) is the same as C(n, n-k)
if (k > n - k) {
k = n - k;
}
int res = 1;
// Calculate the value of n! / (k! * (n-k)!)
for (int i = 0; i < k; ++i) {
res *= (n - i);
res /= (i + 1);
}
return res;
}
// Function to find the nth Catalan number
static int catalan(int n) {
// Calculate C(2n, n)
int c = binomialCoeff(2 * n, n);
// Return the nth Catalan number
return c / (n + 1);
}
public static void main(String[] args) {
int n = 3;
System.out.println(catalan(n));
}
}
# Python code finds the number of Dyck Words of given length
# Function to calculate the binomial coefficient C(n, k)
def binomial_coeff(n, k):
# C(n, k) is the same as C(n, n-k)
if k > n - k:
k = n - k
res = 1
# Calculate the value of n! / (k! * (n-k)!)
for i in range(k):
res *= (n - i)
res //= (i + 1)
return res
# Function to find the nth Catalan number
def catalan(n):
# Calculate C(2n, n)
c = binomial_coeff(2 * n, n)
# Return the nth Catalan number
return c // (n + 1)
n = 3
print(catalan(n))
// C# code finds the number of Dyck Words of given length
using System;
class GfG {
// Function to calculate the binomial
// coefficient C(n, k)
static int BinomialCoeff(int n, int k) {
// C(n, k) is the same as C(n, n-k)
if (k > n - k) {
k = n - k;
}
int res = 1;
// Calculate the value of n! / (k! * (n-k)!)
for (int i = 0; i < k; ++i) {
res *= (n - i);
res /= (i + 1);
}
return res;
}
// Function to find the nth Catalan number
static int Catalan(int n) {
// Calculate C(2n, n)
int c = BinomialCoeff(2 * n, n);
// Return the nth Catalan number
return c / (n + 1);
}
static void Main() {
int n = 3;
Console.WriteLine(Catalan(n));
}
}
// Javascript code finds the number of Dyck Words of given length
// Function to calculate the binomial coefficient C(n, k)
function binomialCoeff(n, k) {
// C(n, k) is the same as C(n, n-k)
if (k > n - k) {
k = n - k;
}
let res = 1;
// Calculate the value of n! / (k! * (n-k)!)
for (let i = 0; i < k; ++i) {
res *= (n - i);
res /= (i + 1);
}
return res;
}
// Function to find the nth Catalan number
function catalan(n) {
// Calculate C(2n, n)
let c = binomialCoeff(2 * n, n);
// Return the nth Catalan number
return c / (n + 1);
}
let n = 3;
console.log(catalan(n));
Output
5
Time Complexity: O(n), where n is nth catalan number.
Auxiliary Space: O(1)