Dyck Words of given length
Given an integer n, the task is to count Dyck words possible of length n. A DYCK word is a word containing only characters ‘X’ and ‘Y’ such that in every prefix of the word frequency(‘X’) ? frequency(‘Y’)
Examples:
Input: n = 2
Output: 2
“XY” and “XX” are the only possible DYCK words of length 2.
Input: n = 5
Output: 42
Approach:
Geometrical Interpretation: Its based upon the idea of DYCK PATH.
The above diagrams represent DYCK PATHS from (0, 0) to (n, n).
A DYCK PATH contains n horizontal line segments and n vertical line segments that doesn’t cross the segment AB.
The main idea behind this problem is to find the total number of DYCK paths from (0, 0) to (n, n).
To approach this problem the main idea is to find the total number of paths of Manhattan Distance between (0, 0) to (n, n) and exclude all those paths that cross the segment AB.
How to calculate the number of paths that cross segment AB?
Let us call all those paths that cross AB as ‘incorrect’. The ‘incorrect’ paths which crosses AB must pass through line CD.
- Take symmetry of point A across line A.
- Draw a symmetrical line of the incorrect line taking reference with CD.
A symmetrical line wrt CD.
FG-Symmetrical line of an incorrect line.
All those lines that crosses AB their symmetrical line that starts at F finishes at G(n-1, n+1).
Hence the number of incorrect lines are :
2 * nCn – 1
Hence number of DYCK words with n ‘X’ and n ‘Y’ is:
2 * nCn – 2 * nCn – 1 = (2 * n)! / (n)! * (n + 1)!
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
long long int count_Dyck_Words(unsigned int n)
{
long long int res = 1;
for ( int i = 0; i < n; ++i) {
res *= (2 * n - i);
res /= (i + 1);
}
return (res / (n + 1));
}
int main()
{
int n = 5;
cout << count_Dyck_Words(n);
return 0;
}
|
Java
class GFG
{
static int count_Dyck_Words( int n)
{
int res = 1 ;
for ( int i = 0 ; i < n; ++i)
{
res *= ( 2 * n - i);
res /= (i + 1 );
}
return (res / (n + 1 ));
}
public static void main(String[] args)
{
int n = 5 ;
System.out.println(count_Dyck_Words(n));
}
}
|
Python3
def count_Dyck_Words(n) :
res = 1 ;
for i in range (n) :
res * = ( 2 * n - i);
res / / = (i + 1 );
return (res / (n + 1 ));
if __name__ = = "__main__" :
n = 5 ;
print (count_Dyck_Words(n));
|
C#
using System;
class GFG
{
static int count_Dyck_Words( int n)
{
int res = 1;
for ( int i = 0; i < n; ++i)
{
res *= (2 * n - i);
res /= (i + 1);
}
return (res / (n + 1));
}
public static void Main()
{
int n = 5;
Console.WriteLine(count_Dyck_Words(n));
}
}
|
PHP
<?php
function count_Dyck_Words( $n )
{
$res = 1;
for ( $i = 0; $i < $n ; ++ $i )
{
$res *= (2 * $n - $i );
$res /= ( $i + 1);
}
return ( $res / ( $n + 1));
}
$n = 5;
echo (count_Dyck_Words( $n ));
?>
|
Javascript
<script>
function count_Dyck_Words(n)
{
let res = 1;
for (let i = 0; i < n; ++i)
{
res *= (2 * n - i);
res = parseInt(res / (i + 1), 10);
}
return parseInt(res / (n + 1), 10);
}
let n = 5;
document.write(count_Dyck_Words(n));
</script>
|
Last Updated :
31 May, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...