Count of Subsequences of given string X in between strings Y and Z
Last Updated :
23 Jul, 2025
Given three strings, 'X', 'Y' and 'Z', the task is to count the number of subsequences of 'X' which is lexicographically greater than or equal to 'Y' and lexicographically lesser than or equal to 'Z'.
Examples:
Input: X = "abc", Y = "a", Z = "bc"
Output: 6
Explanation: The subsequences of X which are greater than or equal to string 'Y' and lesser than or equal to string 'Z' are
{ "a", "b", "ab", "ac", "bc", "abc" }
Input: X = "ade", Y = "a", Z = "dc"
Output: 5
Explanation: The subsequences of X which are greater than or equal to string 'Y' and lesser than or equal to string 'Z' are
{ "a", "d", "ad", "ae", "ade"}
Naive Approach: The simplest approach is to generate all subsequences of string 'X' and check if it is greater than or equal to 'Y' and lesser than or equal to 'Z'.
Time Complexity: O(2N * N)
Auxiliary Space: O(N)
Efficient Approach: The above approach can also be optimized by using Dynamic Programming because it has overlapping subproblems and optimal substructure. The subproblems can be stored in dp[][][][] table using memoization where dp[idx1][idx2][bound1][bound2] stores the answer from the idx1th position of string 'X' till the end and from the idx2th position of string 'Y' and 'Z' till the end, where bound1 is a boolean variable which tells if the subsequence constructed till idx1 is equal to the corresponding substring of 'Y' till idx2 and bound2 is a boolean variable which tells if the subsequence constructed till idx1 is equal to the corresponding substring of 'Z' till idx2.
Follow the steps below to solve the problem:
- Initialize a global multidimensional array dp[100][100][2][2] with all values as -1 that stores the result of each recursive call.
- Define a recursive function, say countOfSubsequence(idx1, idx2, bound1, bound2) by performing the following steps.
- If the value of idx1 = Xsize,
- If idx2 = 0, then the subsequence is empty, hence return 0.
- If bound1 is false, then the subsequence is greater than string 'Y', hence return 1.
- If idx2 < Ysize, the subsequence is equal to string 'Y' until idx2 - 1, but not completely equal, hence return 0.
- If the result of the state dp[idx1][idx2][bound1][bound2] is already computed, return this value dp[idx1][idx2][bound1][bound2].
- In case of the current element being excluded from the subsequence, recursively call the function countOfSubsequence for idx1 + 1.
- To include the current element at idx1 of string 'X' in the subsequence, we have to check for constraints in both string 'Y' and 'Z',
- For string 'Y',
- If bound1 is false, then the current element can be included as the subsequence is already greater than string 'Y'.
- Else if idx2 >= Ysize, the current element can be included because the subsequence is already equal to string 'Y' and additionally some more characters are being added to it.
- Else if X[idx1] >= Y[idx2], by including the current element the current subsequence is lexicographically greater than or equal to string 'Y', and hence can be included.
- If any of the above three conditions are satisfied, then it is possible to include the current element w.r.t string 'Y'.
- If bound1 is true, check for X[idx1] == Y[idx2]. If X[idx1] > Y[idx2], update bound1 to false.
- For string 'Z',
- If bound2 is false, then the current element can be included as the subsequence is already lesser than string 'Z'.
- Else if idx2 < Zsize and X[idx1] <= Z[idx2], by including the current element the current subsequence is lexicographically smaller than or equal to string 'Z', and hence can be included.
- If any of the above two conditions are satisfied, then it is possible to include the current element w.r.t string 'Z'.
- If bound2 is true, check for X[idx1] == Z[idx2]. If X[idx1] < Z[idx2], update bound1 to false.
- After placing the current element at idx1, recursively call the countOfSubsequence function (idx1 + 1, idx2 + 1).
- Print the value returned by the function countOfSubsequence(0, 0, 1, 1) as the result.
Illustration:
X = "ac"
Y = "ab"
Z = "bc"
count(0, 0, 1, 1)
/ (Exclude) \ Can be included (X[0] == Y[0], X[0] < Z[0])
/ \ bound1 = 1, bound2 = 0 (as X[0] < Z[0] )
count(1, 0, 1, 1) count(1, 1, 1, 0)
/ (Exclude) \ Cannot be included / (Exclude) \ Can be included (X[1] > Y[1], X[1] == Z[1])
/ \ X[1] > Y[0] / \ bound1 = 0 (as X[1] > Y[1])
/ \ but X[1] > Z[0] / \ bound2 = 0 (as it was previously also 0)
Returns '0' [""] Returns '0' ["c"] Returns '0' ["a"] Returns '1' ["ac"]
empty subsequence [bound1 = 1, [bound1 = 0]
[idx2 == 0] but idx2 < Y.size()]
Hence the final answer is 1, i.e., "ac".
Below is the implementation of the above approach:
C++
// C++ program for the above approach:
#include <bits/stdc++.h>
using namespace std;
int dp[100][100][2][2];
string X, Y, Z;
int XSize, YSize, ZSize;
// Function to find the count
// of subsequences of 'X' which
// is greater than or equal to 'Y'
// but lesser than or equal to 'Z'.
int countOfSubsequence(
int idx1, int idx2,
bool bound1, bool bound2)
{
// If the string 'X'
// is traversed completely.
if (idx1 == XSize) {
// If subsequence is empty, return 0.
if (idx2 == 0)
return 0;
// If bound1 is false (current subsequence
// is larger than 'Y') or
// idx2 is greater than or
// equal to Ysize, return 1.
if (!bound1 or idx2 >= YSize)
return 1;
// Else return 0.
return 0;
}
// If the state has already
// been computed, return it.
if (dp[idx1][idx2][bound1][bound2] != -1) {
return dp[idx1][idx2][bound1][bound2];
}
// Exclude the current element
// from the subsequence.
int ans = countOfSubsequence(
idx1 + 1, idx2,
bound1, bound2);
// Variable to check if current
// character can be included
// the subsequence by checking
// the strings 'Y' and 'Z'.
int isOk = 0;
// Check for first string
// If bound1 is false,
// it means the current character
// can be included in the
// subsequence as the current
// subsequence is already
// greater than the string 'Y'.
if (!bound1) {
++isOk;
}
// If idx2 >= Ysize,
// the subsequence formed by placing
// current character is of greater length
// than string 'Y', hence can be placed.
// If current character is greater than
// or equal to the corresponding
// character in string 'Y', it can be placed.
else if (idx2 >= YSize or X[idx1] >= Y[idx2]) {
++isOk;
bound1 &= (idx2 < YSize
and X[idx1] == Y[idx2]);
}
// Check for second string
// If bound2 is false,
// it means the current character
// can be included in the subsequence
// as the current subsequence is already
// lesser than the string 'Z'.
if (!bound2) {
++isOk;
}
// If current character is lesser than
// or equal to the corresponding character
// in string 'Z', it can be placed.
else if (idx2 < ZSize
and X[idx1] <= Z[idx2]) {
++isOk;
bound2 &= (X[idx1] == Z[idx2]);
}
// If constraints are met by both string
// 'Y' and 'Z', it is possible to include
// the current character of
// string 'X' in the subsequence.
if (isOk == 2) {
// Increase both idx1 and idx2 by 1.
ans += countOfSubsequence(
idx1 + 1, idx2 + 1,
bound1, bound2);
}
// Return the answer.
return dp[idx1][idx2][bound1][bound2] = ans;
}
// Utility function to find the count
// of subsequences of 'X' which is
// greater than or equal to 'Y'
// but lesser than or equal to 'Z'.
int UtilCountOfSubsequence()
{
// Initialize the dp array with -1.
memset(dp, -1, sizeof dp);
// Calculate the size of strings
//'X', 'Y', and 'Z'.
XSize = X.size();
YSize = Y.size();
ZSize = Z.size();
// Function call
return countOfSubsequence(0, 0, 1, 1);
}
// Driver code
int main()
{
// Input strings 'X', 'Y' and 'Z'.
X = "abc";
Y = "a";
Z = "bc";
// If string 'Y' is greater
// than string 'Z', return 0.
if (Y > Z) {
cout << 0 << endl;
return 0;
}
cout << UtilCountOfSubsequence()
<< endl;
}
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
static int [][][][] dp = new int[100][100][2][2];
static String X, Y, Z;
static int XSize, YSize, ZSize;
// Function to find the count
// of subsequences of 'X' which
// is greater than or equal to 'Y'
// but lesser than or equal to 'Z'.
static int countOfSubsequence(int idx1, int idx2,Boolean bound1, Boolean bound2)
{
// If the string 'X'
// is traversed completely.
if (idx1 == XSize) {
// If subsequence is empty, return 0.
if (idx2 == 0)
return 0;
// If bound1 is false (current subsequence
// is larger than 'Y') or
// idx2 is greater than or
// equal to Ysize, return 1.
if (!bound1 || idx2 >= YSize)
return 1;
// Else return 0.
return 0;
}
// If the state has already
// been computed, return it.
if (dp[idx1][idx2][bound1?1:0][bound2?1:0] != -1) {
return dp[idx1][idx2][bound1?1:0][bound2?1:0];
}
// Exclude the current element
// from the subsequence.
int ans = countOfSubsequence(idx1 + 1, idx2, bound1, bound2);
// Variable to check if current
// character can be included
// the subsequence by checking
// the strings 'Y' and 'Z'.
int isOk = 0;
// Check for first string
// If bound1 is false,
// it means the current character
// can be included in the
// subsequence as the current
// subsequence is already
// greater than the string 'Y'.
if (bound1 == false) {
++isOk;
}
// If idx2 >= Ysize,
// the subsequence formed by placing
// current character is of greater length
// than string 'Y', hence can be placed.
// If current character is greater than
// or equal to the corresponding
// character in string 'Y', it can be placed.
else if (idx2 >= YSize || (int)X.charAt(idx1) >= (int)Y.charAt(idx2)) {
++isOk;
bound1 &= (idx2 < YSize && X.charAt(idx1) == Y.charAt(idx2));
}
// Check for second string
// If bound2 is false,
// it means the current character
// can be included in the subsequence
// as the current subsequence is already
// lesser than the string 'Z'.
if (!bound2) {
++isOk;
}
// If current character is lesser than
// or equal to the corresponding character
// in string 'Z', it can be placed.
else if (idx2 < ZSize && (int)X.charAt(idx1) <= (int)Z.charAt(idx2)) {
++isOk;
bound2 &= (X.charAt(idx1) == Z.charAt(idx2));
}
// If constraints are met by both string
// 'Y' and 'Z', it is possible to include
// the current character of
// string 'X' in the subsequence.
if (isOk == 2) {
// Increase both idx1 and idx2 by 1.
ans += countOfSubsequence(idx1 + 1, idx2 + 1, bound1, bound2);
}
// Return the answer.
return dp[idx1][idx2][bound1?1:0][bound2?1:0] = ans;
}
// Utility function to find the count
// of subsequences of 'X' which is
// greater than or equal to 'Y'
// but lesser than or equal to 'Z'.
static int UtilCountOfSubsequence()
{
// Initialize the dp array with -1.
for(int i=0;i<100;i++){
for(int j=0;j<100;j++){
for(int k=0;k<2;k++){
for(int l=0;l<2;l++){
dp[i][j][k][l] = -1;
}
}
}
}
// Calculate the size of strings
//'X', 'Y', and 'Z'.
XSize = X.length();
YSize = Y.length();
ZSize = Z.length();
// Function call
return countOfSubsequence(0, 0, true , true);
}
// Driver code
public static void main(String args[])
{
// Input strings 'X', 'Y' and 'Z'.
X = "abc";
Y = "a";
Z = "bc";
// If string 'Y' is greater
// than string 'Z', return 0.
if (Y.compareTo(Z) > 0) {
System.out.println(0);
return;
}
System.out.println(UtilCountOfSubsequence());
}
}
// This code is contributed by shinjanpatra
Python3
# Python3 code for the above approach
dp = [None] * 100
for i in range(len(dp)):
dp[i] = [None] * 100
for j in range(len(dp[i])):
dp[i][j] = [None, None]
for k in range(len(dp[i][j])):
dp[i][j][k] = [-1, -1]
X, Y, Z = 0, 0, 0
XSize, YSize, ZSize = 0, 0, 0
# Function to find the count
# of subsequences of 'X' which
# is greater than or equal to 'Y'
# but lesser than or equal to 'Z'.
def countOfSubsequence(idx1, idx2, bound1, bound2):
global X, Y, Z, XSize, YSize, ZSize, dp
# If the string 'X'
# is traversed completely.
if (idx1 == XSize):
# If subsequence is empty, return 0.
if (idx2 == 0):
return 0
# If bound1 is false (current subsequence
# is larger than 'Y') or
# idx2 is greater than or
# equal to Ysize, return 1.
if (not bound1 or idx2 >= YSize):
return 1
# Else return 0.
return 0
# If the state has already
# been computed, return it.
if (dp[idx1][idx2][bound1][bound2] != -1):
return dp[idx1][idx2][bound1][bound2]
# Exclude the current element
# from the subsequence.
ans = countOfSubsequence(idx1 + 1, idx2, bound1,
bound2)
# Variable to check if current
# character can be included
# the subsequence by checking
# the strings 'Y' and 'Z'.
isOk = 0
# Check for first string
# If bound1 is false,
# it means the current character
# can be included in the
# subsequence as the current
# subsequence is already
# greater than the string 'Y'.
if (not bound1):
isOk += 1
# If idx2 >= Ysize,
# the subsequence formed by placing
# current character is of greater length
# than string 'Y', hence can be placed.
# If current character is greater than
# or equal to the corresponding
# character in string 'Y', it can be placed.
elif (idx2 >= YSize or X[idx1] >= Y[idx2]):
isOk += 1
bound1 &= (idx2 < YSize and X[idx1] == Y[idx2])
# Check for second string
# If bound2 is false,
# it means the current character
# can be included in the subsequence
# as the current subsequence is already
# lesser than the string 'Z'.
if (not bound2):
isOk += 1
# If current character is lesser than
# or equal to the corresponding character
# in string 'Z', it can be placed.
elif (idx2 < ZSize and X[idx1] <= Z[idx2]):
isOk += 1
bound2 &= (X[idx1] == Z[idx2])
# If constraints are met by both string
# 'Y' && 'Z', it is possible to include
# the current character of
# string 'X' in the subsequence.
if (isOk == 2):
# Increase both idx1 && idx2 by 1.
ans += countOfSubsequence(idx1 + 1, idx2 + 1,
bound1, bound2)
# Return the answer.
dp[idx1][idx2][bound1][bound2] = ans
return ans
# Utility function to find the count
# of subsequences of 'X' which is
# greater than or equal to 'Y'
# but lesser than or equal to 'Z'.
def UtilCountOfSubsequence():
global X, Y, Z, XSize, YSize, ZSize, dp
# Calculate the size of strings
# 'X', 'Y', and 'Z'.
XSize = len(X)
YSize = len(Y)
ZSize = len(Z)
# Function call
return countOfSubsequence(0, 0, 1, 1)
# Driver code
# Input strings 'X', 'Y' and 'Z'.
X = "abc"
Y = "a"
Z = "bc"
# If string 'Y' is greater
# than string 'Z', return 0.
if (Y > Z):
print(0)
print(UtilCountOfSubsequence())
# This code is contributed by phasing17
C#
// C# program to implement above approach
using System;
using System.Collections.Generic;
class GFG
{
public static int[, , ,] dp = new int[100, 100, 2, 2];
public static String X = "", Y = "", Z = "";
public static int XSize, YSize, ZSize;
// Return 1 if bool is true
// else false
public static int boolToInt(bool input){
if(input) return 1;
return 0;
}
// Function to find the count
// of subsequences of 'X' which
// is greater than or equal to 'Y'
// but lesser than or equal to 'Z'.
public static int countOfSubsequence(int idx1, int idx2, bool bound1, bool bound2)
{
// If the string 'X'
// is traversed completely.
if (idx1 == XSize) {
// If subsequence is empty, return 0.
if (idx2 == 0)
return 0;
// If bound1 is false (current subsequence
// is larger than 'Y') or
// idx2 is greater than or
// equal to Ysize, return 1.
if (!bound1 || idx2 >= YSize)
return 1;
// Else return 0.
return 0;
}
// If the state has already
// been computed, return it.
if (dp[idx1, idx2, boolToInt(bound1), boolToInt(bound2)] != -1) {
return dp[idx1, idx2, boolToInt(bound1), boolToInt(bound2)];
}
// Exclude the current element
// from the subsequence.
int ans = countOfSubsequence(idx1 + 1, idx2, bound1, bound2);
// Variable to check if current
// character can be included
// the subsequence by checking
// the strings 'Y' and 'Z'.
int isOk = 0;
// Check for first string
// If bound1 is false,
// it means the current character
// can be included in the
// subsequence as the current
// subsequence is already
// greater than the string 'Y'.
if (!bound1) {
++isOk;
}
// If idx2 >= Ysize,
// the subsequence formed by placing
// current character is of greater length
// than string 'Y', hence can be placed.
// If current character is greater than
// or equal to the corresponding
// character in string 'Y', it can be placed.
else if (idx2 >= YSize || X[idx1] >= Y[idx2]){
++isOk;
bound1 &= (idx2 < YSize && X[idx1] == Y[idx2]);
}
// Check for second string
// If bound2 is false,
// it means the current character
// can be included in the subsequence
// as the current subsequence is already
// lesser than the string 'Z'.
if (!bound2) {
++isOk;
}
// If current character is lesser than
// or equal to the corresponding character
// in string 'Z', it can be placed.
else if (idx2 < ZSize && X[idx1] <= Z[idx2]) {
++isOk;
bound2 &= (X[idx1] == Z[idx2]);
}
// If constraints are met by both string
// 'Y' and 'Z', it is possible to include
// the current character of
// string 'X' in the subsequence.
if (isOk == 2) {
// Increase both idx1 and idx2 by 1.
ans += countOfSubsequence(idx1 + 1, idx2 + 1, bound1, bound2);
}
// Return the answer.
return dp[idx1, idx2, boolToInt(bound1), boolToInt(bound2)] = ans;
}
// Utility function to find the count
// of subsequences of 'X' which is
// greater than or equal to 'Y'
// but lesser than or equal to 'Z'.
public static int UtilCountOfSubsequence()
{
// Initialize the dp array with -1.
for(int i=0 ; i<100 ; i++){
for(int j=0 ; j<100 ; j++){
for(int k=0 ; k<2 ; k++){
for(int l=0 ; l<2 ; l++){
dp[i, j, k, l] = -1;
}
}
}
}
// Calculate the size of strings
//'X', 'Y', and 'Z'.
XSize = X.Length;
YSize = Y.Length;
ZSize = Z.Length;
// Function call
return countOfSubsequence(0, 0, true, true);
}
// Driver Code
public static void Main(string[] args){
// Input strings 'X', 'Y' and 'Z'.
X = "abc";
Y = "a";
Z = "bc";
// If string 'Y' is greater
// than string 'Z', return 0.
if (Y.CompareTo(Z) > 0) {
Console.Write(0);
return;
}
Console.Write(UtilCountOfSubsequence());
}
}
// This code is contributed by subhamgoyal2014.
JavaScript
<script>
// JavaScript code for the above approach
let dp = new Array(100);
for (let i = 0; i < dp.length; i++) {
dp[i] = new Array(100)
for (let j = 0; j < dp[i].length; j++) {
dp[i][j] = new Array(2)
for (let k = 0; k < dp[i][j].length; k++) {
dp[i][j][k] = new Array(2).fill(-1)
}
}
}
let X, Y, Z;
let XSize, YSize, ZSize;
// Function to find the count
// of subsequences of 'X' which
// is greater than or equal to 'Y'
// but lesser than or equal to 'Z'.
function countOfSubsequence(
idx1, idx2,
bound1, bound2) {
// If the string 'X'
// is traversed completely.
if (idx1 == XSize) {
// If subsequence is empty, return 0.
if (idx2 == 0)
return 0;
// If bound1 is false (current subsequence
// is larger than 'Y') or
// idx2 is greater than or
// equal to Ysize, return 1.
if (!bound1 || idx2 >= YSize)
return 1;
// Else return 0.
return 0;
}
// If the state has already
// been computed, return it.
if (dp[idx1][idx2][bound1][bound2] != -1) {
return dp[idx1][idx2][bound1][bound2];
}
// Exclude the current element
// from the subsequence.
let ans = countOfSubsequence(
idx1 + 1, idx2,
bound1, bound2);
// Variable to check if current
// character can be included
// the subsequence by checking
// the strings 'Y' and 'Z'.
let isOk = 0;
// Check for first string
// If bound1 is false,
// it means the current character
// can be included in the
// subsequence as the current
// subsequence is already
// greater than the string 'Y'.
if (!bound1) {
++isOk;
}
// If idx2 >= Ysize,
// the subsequence formed by placing
// current character is of greater length
// than string 'Y', hence can be placed.
// If current character is greater than
// or equal to the corresponding
// character in string 'Y', it can be placed.
else if (idx2 >= YSize || X[idx1] >= Y[idx2]) {
++isOk;
bound1 &= (idx2 < YSize
&& X[idx1] == Y[idx2]);
}
// Check for second string
// If bound2 is false,
// it means the current character
// can be included in the subsequence
// as the current subsequence is already
// lesser than the string 'Z'.
if (!bound2) {
++isOk;
}
// If current character is lesser than
// or equal to the corresponding character
// in string 'Z', it can be placed.
else if (idx2 < ZSize
&& X[idx1] <= Z[idx2]) {
++isOk;
bound2 &= (X[idx1] == Z[idx2]);
}
// If constraints are met by both string
// 'Y' && 'Z', it is possible to include
// the current character of
// string 'X' in the subsequence.
if (isOk == 2) {
// Increase both idx1 && idx2 by 1.
ans += countOfSubsequence(
idx1 + 1, idx2 + 1,
bound1, bound2);
}
// Return the answer.
return dp[idx1][idx2][bound1][bound2] = ans;
}
// Utility function to find the count
// of subsequences of 'X' which is
// greater than or equal to 'Y'
// but lesser than or equal to 'Z'.
function UtilCountOfSubsequence() {
// Calculate the size of strings
//'X', 'Y', and 'Z'.
XSize = X.length;
YSize = Y.length;
ZSize = Z.length;
// Function call
return countOfSubsequence(0, 0, 1, 1);
}
// Driver code
// Input strings 'X', 'Y' and 'Z'.
X = "abc";
Y = "a";
Z = "bc";
// If string 'Y' is greater
// than string 'Z', return 0.
if (Y > Z) {
document.write(0 + '<br>')
}
document.write(UtilCountOfSubsequence()
+ '<br>')
// This code is contributed by Potta Lokesh
</script>
Time Complexity: O(N2 * 2 * 2)
Auxiliary Space: O(N2 * 2 * 2)
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