Count Possible Decodings of a given Digit Sequence
Last Updated :
02 Dec, 2024
Let 1 represent ‘A’, 2 represent ‘B’, etc. Given a digit sequence, count the number of possible decodings of the given digit sequence. When decoding multiple interpretations can arise due to overlapping possibilities for how digits can be grouped into valid letter mappings. Each mapping corresponds to a letter based on its numeric value (e.g., “1” maps to ‘A’, “2” to ‘B’, …, “26” to ‘Z’).
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”
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 countDecodes(string digits) {
return decodeHelper(digits, 0);
}
int main() {
string digits = "121";
cout << countDecodes(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 countDecodes(String digits) {
return decodeHelper(digits, 0);
}
public static void main(String[] args) {
String digits = "121";
System.out.println(countDecodes(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 countDecodes(digits):
return decodeHelper(digits, 0)
if __name__ == "__main__":
digits = "121"
print(countDecodes(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 CountDecodes(string digits) {
return DecodeHelper(digits, 0);
}
static void Main() {
string digits = "121";
Console.WriteLine(CountDecodes(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 countDecodes(digits) {
return decodeHelper(digits, 0);
}
const digits = "121";
console.log(countDecodes(digits));
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 countDecodes(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 << countDecodes(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 countDecodes(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(countDecodes(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 countDecodes(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(countDecodes(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 CountDecodes(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(CountDecodes(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 countDecodes(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(countDecodes(digits));
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 countDecodes(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 << countDecodes(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 countDecodes(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(countDecodes(digits));
}
}
Python
# Python program to count decoding ways of a digit string
# using Tabulation
# Function to count decoding ways for the entire string.
def count_decodes(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(count_decodes(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 CountDecodes(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(CountDecodes(digits));
}
}
JavaScript
// Javascript program to count decoding ways of
// a digit string using Tabulation
// Function to count decoding ways for the entire string.
function countDecodes(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(countDecodes(digits));
Unlock your potential with our DSA Self-Paced course, designed to help you master Data Structures and Algorithms at your own pace. In 90 days, you’ll learn the core concepts of DSA, tackle real-world problems, and boost your problem-solving skills, all at a speed that fits your schedule. With comprehensive lessons and practical exercises, this course will set you up for success in technical interviews and beyond.
And here's the challenge – join the Three 90 Challenge! Complete 90% of the course in 90 days, and you’ll earn a 90% refund. It's a great way to stay motivated, track your progress, and reward yourself for your dedication. Start the challenge today, and push your limits to achieve mastery in DSA!
Similar Reads
Count Possible Decodings of a given Digit Sequence
Let 1 represent 'A', 2 represent 'B', etc. Given a digit sequence, count the number of possible decodings of the given digit sequence. When decoding multiple interpretations can arise due to overlapping possibilities for how digits can be grouped into valid letter mappings. Each mapping corresponds
15+ min read
Count possible decodings of a given Digit Sequence | Set 2
Given an encoded string str consisting of digits and * which can be filled by any digit 1 - 9, the task is to find the number of ways to decode that string into a sequence of alphabets A-Z. Note: The input string contains number from 0-9 and character '*' only. Examples: Input: str = "1*" Output: 18
12 min read
Print all Possible Decodings of a given Digit Sequence
Given the numeric string str, where 1 represents 'a', 2 represents 'b', ..., 26 represents 'z', the task is to print all possible alphabetical strings that can be obtained from str. Examples: Input: str = "1123" Output: aabc kbc alc aaw kw Explanation: The given string can be splitted as: 1) "1123"
11 min read
Count possible decoding of a given digit sequence with hidden characters
Given a string S containing digits and character '*' i.e. hidden character, the task is to find the number of ways to decode this hidden character of the given string. Since the answer may be very large, return it modulo 109+7. A string containing letters from A-Z can be encoded into numbers using t
15+ min read
Count Possible Decodings of a given Digit Sequence in O(N) time and Constant Auxiliary space
Given a digit sequence S, the task is to find the number of possible decodings of the given digit sequence where 1 represents 'A', 2 represents 'B' ... and so on up to 26, where 26 represents 'Z'. Examples: Input: S = "121" Output: 3 The possible decodings are "ABA", "AU", "LA" Input: S = "1234" Out
6 min read
Count of N length Strings having S as a Subsequence
Given a string S and an integer N, the task is to calculate the number of strings of length N consisting of only lowercase characters which have S as one of its subsequences. Note: As the answer can be very large return it modulo 10^9+7. Examples: Input: N = 3, S = "abc"Output: 1Explanation: There a
10 min read
Count of all possible numbers not exceeding M having suffix N
Given two positive integers N and M, the task is to find the count of all possible numbers in the range [1, M], having suffix as N. Examples: Input: N = 5, M = 15 Output: 2 Explanation: Only numbers satisfying the conditions are {5, 15}.Input: N = 25, M = 4500 Output : 45 Naive Approach: The simples
4 min read
Count sequences of positive integers having product X
Given an array arr[] of size N, the task is to find the total number of sequences of positive integers possible (greater than 1) whose product is exactly X. The value of X is calculated as the product of the terms where ith term is generated by raising ith prime number to the power of arr[i]. X = 2
9 min read
Count N-digit numbers possible consisting of digits X and Y
Given three integers N, X, and Y, the task is to find the count of N-digit numbers that can be formed using digits 0 to 9 satisfying the following conditions: Digits X and Y must be present in them.The number may contain leading 0s. Note: Since the answer can be very large, print the answer modulo 1
6 min read
Count of N digit numbers possible which satisfy the given conditions
Given an integer N, the Task is to find the total number of N digit numbers possible such that: All the digits of the numbers are from the range [0, N].There are no leading 0s.All the digits in a number are distinct. Examples: Input: N = 2 Output: 4 10, 12, 20 and 21 are the only possible 2 digit nu
4 min read
Count all possible N digit numbers that satisfy the given condition
Given an integer N, the task is to count all possible N digit numbers such that A + reverse(A) = 10N - 1 where A is an N digit number and reverse(A) is reverse of A. A shouldn't have any leading 0s.Examples: Input: N = 2 Output: 9 All possible 2 digit numbers are 90, 81, 72, 63, 54, 45, 36, 27 and 1
4 min read
Count Distinct Subsequences
Given a string, find the count of distinct subsequences of it. Examples: Input: str = "gfg"Output: 7Explanation: The seven distinct subsequences are "", "g", "f", "gf", "fg", "gg" and "gfg" Input: str = "ggg"Output: 4Explanation: The four distinct subsequences are "", "g", "gg" and "ggg" The problem
15+ min read
Count of unique digits in a given number N
Given a number N, the task is to count the number of unique digits in the given number. Examples: Input: N = 22342 Output: 2 Explanation:The digits 3 and 4 occurs only once. Hence, the output is 2. Input: N = 99677 Output: 1Explanation:The digit 6 occurs only once. Hence, the output is 1. Naive Appr
6 min read
Counting numbers with given digits and digit sum
Given a number N, count the numbers X of length exactly N such that the number X and the sum of digits of the number X have digits A and B only in their decimal representation. The length of a number is defined as the number of digits in its decimal representation without leading zeroes. Note: As th
11 min read
Count of repeating digits in a given Number
Given a number N, the task is to count the total number of repeating digits in the given number. Examples: Input: N = 99677 Output: 2Explanation:In the given number only 9 and 7 are repeating, hence the answer is 2. Input: N = 12Output: 0Explanation:In the given number no digits are repeating, hence
12 min read
Count of subsequence of an Array having all unique digits
Given an array A containing N positive integers, the task is to find the number of subsequences of this array such that in each subsequence , no digit is repeated twice, i.e. all the digits of the subsequences must be unique.Examples: Input: A = [1, 12, 23, 34] Output: 7 The subsequences are: {1}, {
15+ min read
Count of N-digit numbers having digit XOR as single digit
Given an integer N, the task is to find the total count of N-Digit numbers such that the Bitwise XOR of the digits of the numbers is a single digit. Examples: Input: N = 1Output: 9Explanation: 1, 2, 3, 4, 5, 6, 7, 8, 9 are the numbers. Input: N = 2Output: 66Explanation: There are 66 such 2-digit num
14 min read
Count all increasing subsequences
We are given an array of digits (values lie in range from 0 to 9). The task is to count all the sub sequences possible in array such that in each subsequence every digit is greater than its previous digits in the subsequence. Examples: Input : arr[] = {1, 2, 3, 4}Output: 15Explanation: There are 15
8 min read
Count of distinct possible strings after performing given operations
Given a numeric string S consisting of only three types of characters 0, 1, and 2 initially and following two operations: The occurrence of two consecutive 1 can be replaced by 3.The occurrence of two consecutive 2 can be replaced by 4.The given task is to find the total number of different strings
10 min read