Count Possible Decodings of a given Digit Sequence
Last Updated :
22 Mar, 2025
Let 1 maps to 'A', 2 maps to 'B', ..., 26 to 'Z'. Given a digit sequence, count the number of possible decodings of the given digit sequence.
Consider the input string "123". There are three valid ways to decode it:
- "ABC": The grouping is (1, 2, 3) → 'A', 'B', 'C'
- "AW": The grouping is (1, 23) → 'A', 'W'
- "LC": The grouping is (12, 3) → 'L', 'C'
Note: Groupings that contain invalid codes (e.g., "0" by itself or numbers greater than "26") are not allowed.
For instance, the string "230" is invalid because "0" cannot stand alone, and "30" is greater than "26", so it cannot represent any letter. The task is to find the total number of valid ways to decode a given string.
Examples:
Input: digits = "121"
Output: 3
Explanation: The possible decodings are "ABA", "AU", "LA"
Input: digits = "1234"
Output: 3
Explanation: The possible decodings are "ABCD", "LCD", "AWD"
[Naive Approach] Using Recursion - O(2^n) Time and O(n) Space
For the recursive approach to count decoding ways, there will be two cases:
- If the current digit is not '0', the problem reduces to solving for the remaining digits starting from the next index.
- If the two digits form a valid number between 10 and 26, the problem reduces to solving for the digits starting two positions ahead.
The recurrence relation will look like this:
- decodeHelper(digits, index) = decodeHelper(digits, index + 1) + decodeHelper(digits, index + 2)
Base Case: if index >= digits.length()
- decodeHelper(digits, index) = 1
C++
// C++ program to count decoding ways of a digit string
// using recursion.
#include <bits/stdc++.h>
using namespace std;
// Helper function to recursively calculate decoding ways.
int decodeHelper(string &digits, int index)
{
int n = digits.length();
// Base case: If we reach the end of the string,
// return 1 as it signifies a valid decoding.
if (index >= n)
{
return 1;
}
int ways = 0;
// Single-digit decoding: check if current digit is not '0'.
if (digits[index] != '0')
{
ways = decodeHelper(digits, index + 1);
}
// Two-digit decoding: check if next two digits are valid.
if ((index + 1 < n) && ((digits[index] == '1' && digits[index + 1] <= '9') ||
(digits[index] == '2' && digits[index + 1] <= '6')))
{
ways += decodeHelper(digits, index + 2);
}
return ways;
}
// Function to count decoding ways for the
// entire string.
int countWays(string &digits)
{
return decodeHelper(digits, 0);
}
int main()
{
string digits = "121";
cout << countWays(digits) << endl;
return 0;
}
Java
// Java program to count decoding ways of a digit string
// using recursion.
import java.util.*;
class GfG {
// Helper function to recursively calculate decoding
// ways.
static int decodeHelper(String digits, int index)
{
int n = digits.length();
// Base case: If we reach the end of the string,
// return 1 as it signifies a valid decoding.
if (index >= n) {
return 1;
}
int ways = 0;
// Single-digit decoding: check if current digit is
// not '0'.
if (digits.charAt(index) != '0') {
ways = decodeHelper(digits, index + 1);
}
// Two-digit decoding: check if next two digits are
// valid.
if ((index + 1 < n)
&& ((digits.charAt(index) == '1'
&& digits.charAt(index + 1) <= '9')
|| (digits.charAt(index) == '2'
&& digits.charAt(index + 1) <= '6'))) {
ways += decodeHelper(digits, index + 2);
}
return ways;
}
// Function to count decoding ways for the
// entire string.
static int countWays(String digits)
{
return decodeHelper(digits, 0);
}
public static void main(String[] args)
{
String digits = "121";
System.out.println(countWays(digits));
}
}
Python
# Python program to count decoding ways of a digit string
# using recursion.
# Helper function to recursively calculate decoding ways
def decodeHelper(digits, index):
n = len(digits)
# Base case: If we reach the end of the string,
# return 1 as it signifies a valid decoding.
if index >= n:
return 1
ways = 0
# Single-digit decoding: check if current digit is not '0'.
if digits[index] != '0':
ways = decodeHelper(digits, index + 1)
# Two-digit decoding: check if next two digits are valid.
if (index + 1 < n and
((digits[index] == '1' and digits[index + 1] <= '9') or
(digits[index] == '2' and digits[index + 1] <= '6'))):
ways += decodeHelper(digits, index + 2)
return ways
# Function to count decoding ways for the entire string
def countWays(digits):
return decodeHelper(digits, 0)
if __name__ == "__main__":
digits = "121"
print(countWays(digits))
C#
// C# program to count decoding ways of a digit string
// using recursion.
using System;
class GfG {
// Helper function to recursively calculate decoding
// ways.
static int DecodeHelper(string digits, int index)
{
int n = digits.Length;
// Base case: If we reach the end of the string,
// return 1 as it signifies a valid decoding.
if (index >= n) {
return 1;
}
int ways = 0;
// Single-digit decoding: check if current digit is
// not '0'.
if (digits[index] != '0') {
ways = DecodeHelper(digits, index + 1);
}
// Two-digit decoding: check if next two digits are
// valid.
if ((index + 1 < n)
&& ((digits[index] == '1'
&& digits[index + 1] <= '9')
|| (digits[index] == '2'
&& digits[index + 1] <= '6'))) {
ways += DecodeHelper(digits, index + 2);
}
return ways;
}
// Function to count decoding ways for the entire
// string.
static int countWays(string digits)
{
return DecodeHelper(digits, 0);
}
static void Main()
{
string digits = "121";
Console.WriteLine(countWays(digits));
}
}
JavaScript
// JavaScript program to count decoding ways of a digit
// string using recursion.
function decodeHelper(digits, index)
{
const n = digits.length;
// Base case: If we reach the end of the string,
// return 1 as it signifies a valid decoding.
if (index >= n) {
return 1;
}
let ways = 0;
// Single-digit decoding: check if current digit is not
// '0'.
if (digits[index] !== "0") {
ways = decodeHelper(digits, index + 1);
}
// Two-digit decoding: check if next two digits are
// valid.
if (index + 1 < n
&& ((digits[index] === "1"
&& digits[index + 1] <= "9")
|| (digits[index] === "2"
&& digits[index + 1] <= "6"))) {
ways += decodeHelper(digits, index + 2);
}
return ways;
}
// Function to count decoding ways for the entire string
function countWays(digits)
{
return decodeHelper(digits, 0);
}
const digits = "121";
console.log(countWays(digits));
[Better Approach 1] Using Top-Down DP (Memoization) - O(n) Time and O(n) Space
1. Optimal Substructure: The solution to the problem can be broken down into smaller subproblems:
Mathematically, the recurrence relations are:
if digits[index] != '0':
decodeHelper(digits, index) = decodeHelper(digits, index + 1)
if digits[index] and digits[index + 1] form a valid number :
decodeHelper(digits, index) += decodeHelper(digits, index + 2)
2. Overlapping Subproblems: The same subproblems are recalculated multiple times, like computing the number of decodings from index i repeatedly. This overlap can be avoided using memoization to store already computed results.
memo[index] = decodeHelper(digits, index + 1) if digits[index] != '0'
memo[index] += decodeHelper(digits, index + 2) if digits[index] and digits[index + 1]
C++
// C++ program to count decoding ways of a digit string
// using Memoization
#include <bits/stdc++.h>
using namespace std;
// Helper function to recursively calculate decoding ways
// with memoization.
int decodeHelper(string &digits, int index, vector<int> &memo)
{
int n = digits.length();
// Base case: If we reach the end of the string,
// return 1 as it signifies a valid decoding.
if (index >= n)
{
return 1;
}
// If the value is already calculated for this
// index, return it.
if (memo[index] != -1)
{
return memo[index];
}
int ways = 0;
// Single-digit decoding: check if current digit is not '0'.
if (digits[index] != '0')
{
ways = decodeHelper(digits, index + 1, memo);
}
// Two-digit decoding: check if next two digits are valid.
if ((index + 1 < n) && ((digits[index] == '1' && digits[index + 1] <= '9') ||
(digits[index] == '2' && digits[index + 1] <= '6')))
{
ways += decodeHelper(digits, index + 2, memo);
}
// Memoize the result for the current index.
memo[index] = ways;
return ways;
}
// Function to count decoding ways for the entire string.
int countWays(string &digits)
{
int n = digits.length();
// Create a memoization vector initialized to -1.
vector<int> memo(n, -1);
return decodeHelper(digits, 0, memo);
}
int main()
{
string digits = "121";
cout << countWays(digits) << endl;
return 0;
}
Java
// Java program to count decoding ways of a digit string
// using recursion with memoization.
import java.util.*;
class GfG {
// Helper function to recursively calculate decoding
// ways with memoization.
static int decodeHelper(String digits, int index,
int[] memo)
{
int n = digits.length();
// Base case: If we reach the end of the string,
// return 1 as it signifies a valid decoding.
if (index >= n) {
return 1;
}
// If the value is already calculated for this
// index, return it.
if (memo[index] != -1) {
return memo[index];
}
int ways = 0;
// Single-digit decoding: check if current digit is
// not '0'.
if (digits.charAt(index) != '0') {
ways = decodeHelper(digits, index + 1, memo);
}
// Two-digit decoding: check if next two digits are
// valid.
if ((index + 1 < n)
&& ((digits.charAt(index) == '1'
&& digits.charAt(index + 1) <= '9')
|| (digits.charAt(index) == '2'
&& digits.charAt(index + 1) <= '6'))) {
ways += decodeHelper(digits, index + 2, memo);
}
// Memoize the result for the current index.
memo[index] = ways;
return ways;
}
// Function to count decoding ways for the entire
// string.
static int countWays(String digits)
{
int n = digits.length();
// Create a memoization array initialized to -1.
int[] memo = new int[n];
Arrays.fill(memo, -1);
return decodeHelper(digits, 0, memo);
}
public static void main(String[] args)
{
String digits = "121";
System.out.println(countWays(digits));
}
}
Python
# Python program to count decoding ways of a digit string
# using recursion with memoization.
# Helper function to recursively calculate decoding ways
def decodeHelper(digits, index, memo):
n = len(digits)
# Base case: If we reach the end of the string,
# return 1 as it signifies a valid decoding.
if index >= n:
return 1
# If the result for this index is already
# calculated, return it.
if memo[index] != -1:
return memo[index]
ways = 0
# Single-digit decoding: check if current digit is not '0'.
if digits[index] != '0':
ways = decodeHelper(digits, index + 1, memo)
# Two-digit decoding: check if next two digits are valid.
if (index + 1 < n and
((digits[index] == '1' and digits[index + 1] <= '9') or
(digits[index] == '2' and digits[index + 1] <= '6'))):
ways += decodeHelper(digits, index + 2, memo)
# Memoize the result for the current index.
memo[index] = ways
return ways
# Function to count decoding ways for the entire string
def countWays(digits):
n = len(digits)
# Create a memoization list initialized to -1.
memo = [-1] * n
return decodeHelper(digits, 0, memo)
if __name__ == "__main__":
digits = "121"
print(countWays(digits))
C#
// C# program to count decoding ways of a digit string
// using recursion with memoization.
using System;
class GfG {
// Helper function to recursively calculate
// decoding ways.
static int DecodeHelper(string digits, int index,
int[] memo)
{
int n = digits.Length;
// Base case: If we reach the end of the string,
// return 1 as it signifies a valid decoding.
if (index >= n) {
return 1;
}
// If result for this index is already computed,
// return it.
if (memo[index] != -1) {
return memo[index];
}
int ways = 0;
// Single-digit decoding: check if current
// digit is not '0'.
if (digits[index] != '0') {
ways = DecodeHelper(digits, index + 1, memo);
}
// Two-digit decoding: check if next two
// digits are valid.
if ((index + 1 < n)
&& ((digits[index] == '1'
&& digits[index + 1] <= '9')
|| (digits[index] == '2'
&& digits[index + 1] <= '6'))) {
ways += DecodeHelper(digits, index + 2, memo);
}
// Store the result in memo and return it.
memo[index] = ways;
return ways;
}
// Function to count decoding ways for the entire
// string.
static int countWays(string digits)
{
int n = digits.Length;
// Memoization array initialized to -1.
int[] memo = new int[n];
Array.Fill(memo, -1);
return DecodeHelper(digits, 0, memo);
}
static void Main()
{
string digits = "121";
Console.WriteLine(countWays(digits));
}
}
JavaScript
// JavaScript program to count decoding ways of a digit
// string using recursion with memoization.
function decodeHelper(digits, index, memo)
{
const n = digits.length;
// Base case: If we reach the end of the string,
// return 1 as it signifies a valid decoding.
if (index >= n) {
return 1;
}
// If result for this index is already computed, return
// it.
if (memo[index] !== -1) {
return memo[index];
}
let ways = 0;
// Single-digit decoding: check if current digit is not
// '0'.
if (digits[index] !== "0") {
ways = decodeHelper(digits, index + 1, memo);
}
// Two-digit decoding: check if next two digits are
// valid.
if (index + 1 < n
&& ((digits[index] === "1"
&& digits[index + 1] <= "9")
|| (digits[index] === "2"
&& digits[index + 1] <= "6"))) {
ways += decodeHelper(digits, index + 2, memo);
}
// Store the result in memo and return it.
memo[index] = ways;
return ways;
}
// Function to count decoding ways for the entire string.
function countWays(digits)
{
const n = digits.length;
// Memoization array initialized to -1.
const memo = new Array(n).fill(-1);
return decodeHelper(digits, 0, memo);
}
const digits = "121";
console.log(countWays(digits));
[Better Approach 2] Using Bottom-Up DP (Tabulation) - O(n) Time and O(n) Space
The approach here is similar to the recursive method, but instead of breaking down the problem recursively, we solve it iteratively in a bottom-up manner using dynamic programming. We will create a 1D array dp of size (n + 1), where dp[i], represents the number of ways to decode the substring starting from index i of the string digits[].
Dynamic Programming Relation:
If the current digit is not '0', it can be decoded as a single digit, so we update the dp[i] as:
If the next two digits form a valid number between 10 and 26, then we also consider decoding the current and next digit together:
Base Case: dp[n] = 1, where an empty string has one valid decoding.
C++
// C++ program to count decoding ways of a digit string
// using Tabulation
#include <bits/stdc++.h>
using namespace std;
// Function to count decoding ways for the entire string.
int countWays(string &digits)
{
int n = digits.length();
// Create a dp array initialized to 0, with size n + 1.
vector<int> dp(n + 1, 0);
// Base case: An empty string has one valid decoding.
dp[n] = 1;
// Iterate from the end of the string to the beginning.
for (int i = n - 1; i >= 0; i--)
{
// Single-digit decoding: check if current
// digit is not '0'.
if (digits[i] != '0')
{
dp[i] = dp[i + 1];
}
// Two-digit decoding: check if next two digits are valid.
if ((i + 1 < n) &&
((digits[i] == '1' && digits[i + 1] <= '9') || (digits[i] == '2' && digits[i + 1] <= '6')))
{
dp[i] += dp[i + 2];
}
}
return dp[0];
}
int main()
{
string digits = "121";
cout << countWays(digits) << endl;
return 0;
}
Java
// Java program to count decoding ways of a digit string
// using Tabulation
import java.util.ArrayList;
class GfG {
// Function to count decoding ways for the entire
// string.
public static int countWays(String digits)
{
int n = digits.length();
// Create a dp list initialized to 0, with size n
// + 1.
ArrayList<Integer> dp = new ArrayList<>();
for (int i = 0; i <= n; i++) {
dp.add(0);
}
// Base case: An empty string has one valid
// decoding.
dp.set(n, 1);
// Iterate from the end of the string to the
// beginning.
for (int i = n - 1; i >= 0; i--) {
// Single-digit decoding: check if current
// digit is not '0'.
if (digits.charAt(i) != '0') {
dp.set(i, dp.get(i + 1));
}
// Two-digit decoding: check if next two digits
// are valid.
if ((i + 1 < n)
&& ((digits.charAt(i) == '1'
&& digits.charAt(i + 1) <= '9')
|| (digits.charAt(i) == '2'
&& digits.charAt(i + 1) <= '6'))) {
dp.set(i, dp.get(i) + dp.get(i + 2));
}
}
return dp.get(0);
}
public static void main(String[] args)
{
String digits = "121";
System.out.println(countWays(digits));
}
}
Python
# Python program to count decoding ways of a digit string
# using Tabulation
# Function to count decoding ways for the entire string.
def countWays(digits):
n = len(digits)
# Create a dp list initialized to 0, with size n + 1.
dp = [0] * (n + 1)
# Base case: An empty string has one valid decoding.
dp[n] = 1
# Iterate from the end of the string to the beginning.
for i in range(n - 1, -1, -1):
# Single-digit decoding: check if current
# digit is not '0'.
if digits[i] != '0':
dp[i] = dp[i + 1]
# Two-digit decoding: check if next two digits are valid.
if (i + 1 < n and
((digits[i] == '1' and digits[i + 1] <= '9') or
(digits[i] == '2' and digits[i + 1] <= '6'))):
dp[i] += dp[i + 2]
return dp[0]
if __name__ == "__main__":
digits = "121"
print(countWays(digits))
C#
// C# program to count decoding ways of a digit string
// using Tabulation
using System;
using System.Collections.Generic;
class GfG {
// Function to count decoding ways for the entire
// string.
static int countWays(string digits)
{
int n = digits.Length;
// Create a dp list initialized to 0, with size n
// + 1.
List<int> dp = new List<int>(new int[n + 1]);
// Base case: An empty string has one valid
// decoding.
dp[n] = 1;
// Iterate from the end of the string to the
// beginning.
for (int i = n - 1; i >= 0; i--) {
// Single-digit decoding: check if current
// digit is not '0'.
if (digits[i] != '0') {
dp[i] = dp[i + 1];
}
// Two-digit decoding: check if next two digits
// are valid.
if ((i + 1 < n)
&& ((digits[i] == '1'
&& digits[i + 1] <= '9')
|| (digits[i] == '2'
&& digits[i + 1] <= '6'))) {
dp[i] += dp[i + 2];
}
}
return dp[0];
}
static void Main()
{
string digits = "121";
Console.WriteLine(countWays(digits));
}
}
JavaScript
// Javascript program to count decoding ways of
// a digit string using Tabulation
// Function to count decoding ways for the entire string.
function countWays(digits)
{
const n = digits.length;
// Create a dp array initialized to 0, with size n + 1.
const dp = Array(n + 1).fill(0);
// Base case: An empty string has one valid decoding.
dp[n] = 1;
// Iterate from the end of the string to the beginning.
for (let i = n - 1; i >= 0; i--) {
// Single-digit decoding: check if current
// digit is not '0'.
if (digits[i] !== "0") {
dp[i] = dp[i + 1];
}
// Two-digit decoding: check if next two digits are
// valid.
if (i + 1 < n
&& ((digits[i] === "1" && digits[i + 1] <= "9")
|| (digits[i] === "2"
&& digits[i + 1] <= "6"))) {
dp[i] += dp[i + 2];
}
}
return dp[0];
}
const digits = "121";
console.log(countWays(digits));
[Expected Approach] Using Space Optimised DP - O(n) Time and O(1) Space
In previous approach of dynamic programming we have derive the relation between states as given below:
- dp[i] = dp[i + 1]
- dp[i] = dp[i]+ dp[i + 2]
If we observe that for calculating current dp[i] state we only need previous two values dp[i+1] and dp[i]+dp[i+2]. There is no need to store all the previous states.
C++
#include <bits/stdc++.h>
using namespace std;
// Function to count decoding ways for the
// entire string with optimized space.
int countWays(string digits)
{
int n = digits.length();
// If the string is empty or starts with '0',
// there are no valid decodings.
if (n == 0 || digits[0] == '0')
{
return 0;
}
// two variables to store the previous two results.
int prev1 = 1, prev2 = 0;
for (int i = 1; i <= n; ++i)
{
int current = 0;
// Check for valid single-digit decoding
if (digits[i - 1] != '0')
{
current += prev1;
}
// Check for valid two-digit decoding
// (previous digit and current digit form a
// valid number between 10 and 26)
if (i > 1)
{
int twoDigit = (digits[i - 2] - '0') * 10 + (digits[i - 1] - '0');
if (twoDigit >= 10 && twoDigit <= 26)
{
current += prev2;
}
}
// Update prev1 and prev2 for the next iteration.
prev2 = prev1;
prev1 = current;
}
return prev1;
}
int main()
{
string digits = "121";
cout << countWays(digits) << endl;
return 0;
}
Java
// Function to count decoding ways for the entire string
// with optimized space.
class GfG {
static int countWays(String digits)
{
int n = digits.length();
// If the string is empty or starts with '0',
// there are no valid decodings.
if (n == 0 || digits.charAt(0) == '0') {
return 0;
}
// two variables to store the previous two results.
int prev1 = 1, prev2 = 0;
for (int i = 1; i <= n; ++i) {
int current = 0;
// Check for valid single-digit decoding
if (digits.charAt(i - 1) != '0') {
current += prev1;
}
// Check for valid two-digit decoding
// (previous digit and current digit form
// a valid number between 10 and 26)
if (i > 1) {
int twoDigit
= (digits.charAt(i - 2) - '0') * 10
+ (digits.charAt(i - 1) - '0');
if (twoDigit >= 10 && twoDigit <= 26) {
current += prev2;
}
}
// Update prev1 and prev2 for the next
// iteration.
prev2 = prev1;
prev1 = current;
}
return prev1;
}
public static void main(String[] args)
{
String digits = "121";
System.out.println(countWays(digits));
}
}
Python
def countWays(digits):
n = len(digits)
# If the string is empty or starts with '0',
# there are no valid decodings.
if n == 0 or digits[0] == '0':
return 0
# two variables to store the previous two results.
prev1, prev2 = 1, 0
for i in range(1, n + 1):
current = 0
# Check for valid single-digit decoding
if digits[i - 1] != '0':
current += prev1
# Check for valid two-digit decoding
# (previous digit and current digit form a valid number between 10 and 26)
if i > 1:
two_digit = (int(digits[i - 2]) * 10 + int(digits[i - 1]))
if 10 <= two_digit <= 26:
current += prev2
# Update prev1 and prev2 for the next iteration.
prev2 = prev1
prev1 = current
return prev1
if __name__ == '__main__':
digits = '121'
print(countWays(digits))
C#
using System;
class GfG {
static int countWays(string digits)
{
int n = digits.Length;
// If the string is empty or starts with '0',
// there are no valid decodings.
if (n == 0 || digits[0] == '0') {
return 0;
}
// two variables to store the previous two results.
int prev1 = 1, prev2 = 0;
for (int i = 1; i <= n; ++i) {
int current = 0;
// Check for valid single-digit decoding
if (digits[i - 1] != '0') {
current += prev1;
}
// Check for valid two-digit decoding
// (previous digit and current digit form a
// valid number between 10 and 26)
if (i > 1) {
int twoDigit = (digits[i - 2] - '0') * 10
+ (digits[i - 1] - '0');
if (twoDigit >= 10 && twoDigit <= 26) {
current += prev2;
}
}
// Update prev1 and prev2 for the next
// iteration.
prev2 = prev1;
prev1 = current;
}
return prev1;
}
public static void Main()
{
string digits = "121";
Console.WriteLine(countWays(digits));
}
}
JavaScript
function countWays(digits)
{
const n = digits.length;
// If the string is empty or starts with '0',
// there are no valid decodings.
if (n === 0 || digits[0] === "0") {
return 0;
}
// two variables to store the previous two results.
let prev1 = 1, prev2 = 0;
for (let i = 1; i <= n; ++i) {
let current = 0;
// Check for valid single-digit decoding
if (digits[i - 1] !== "0") {
current += prev1;
}
// Check for valid two-digit decoding
// (previous digit and current digit form a valid
// number between 10 and 26)
if (i > 1) {
const twoDigit = (parseInt(digits[i - 2]) * 10
+ parseInt(digits[i - 1]));
if (twoDigit >= 10 && twoDigit <= 26) {
current += prev2;
}
}
// Update prev1 and prev2 for the next iteration.
prev2 = prev1;
prev1 = current;
}
return prev1;
}
const digits = "121";
console.log(countWays(digits));
Similar Reads
Basics & Prerequisites
Data Structures
Array Data Structure GuideIn 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