Count possible decoding of a given digit sequence with hidden characters
Last Updated :
09 May, 2022
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 the following mapping:
'A' -> "1"
'B' -> "2"
'C' -> "3"
'D' -> "4"
...
...
'Z' -> "26"
Note: Characters including 0 are not included in the problem like (J ? 10).
Examples:
Input: s = "*"
Output: 9
Explanation: The encoded message can represent any of the encoded messages "1", "2", "3", "4", "5", "6", "7", "8", or "9".
Each of these can be decoded to the strings "A", "B", "C", "D", "E", "F", "G", "H", and "I" respectively.
Hence, there are a total of 9 ways to decode "*".
Input: s = "1*"
Output: 18
Explanation: The encoded message can represent any of the encoded messages "11", "12", "13", "14", "15", "16", "17", "18", or "19".
Each of these encoded messages have 2 ways to be decoded (e.g. "11" can be decoded to "AA" or "K").
Hence, there are a total of 9 × 2 = 18 ways to decode "1*".
Approach:
This problem can be solved by observing that any constant number can be either decoded in a character which is a single-digit number or it can be decoded into a double-digit number if (i-1)th character is '1' or (i-1)th character is '2' and ith character is between 1 and 6. Therefore, the current state depends on the previous state, and dynamic programming can be used to solve the problem. Follow the steps below to solve the problem:
1. Let dp[i] represent the number of ways to decode the string characters from 0 to i.
2. If the ith character is '*' :
- dp[i] = dp[i-1]*9 considering '*' can be 1 to 9, and it is considered alone as a character.
- Now, if the i and i-1 characters are combined, then,
- If (i-1)th character is '*' then the two “**” together can form 15 possible characters(like 13 will form character 'M'), so we add 15×dp[i-2] to dp[i].
- If (i-1)th character is '1' then dp[i] = dp[i] + 9×dp[i-2] because the possible characters that can be decoded will be 11 to 19(K to S).
- If (i-1)th character is '2' then dp[i] = dp[i] + 6×dp[i-2] as it can take value from 21 to 26.
3. If the ith character is not '*':
- dp[i] = dp[i] + dp[i-1] considering ith character alone as a number.
- Now, if it is possible to combine (i-1)th character and ith character together then add dp[i-2] to dp[i].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int M = 1000000007;
int waysOfDecoding(string s)
{
vector<int> dp((int)s.size()+1);
dp[0] = 1;
// check the first character of the string
// if it is '*' then 9 ways
dp[1] = s[0] == '*'
? 9
: s[0] == '0' ? 0 : 1;
// traverse the string
for (int i = 1; i < (int)s.size(); i++) {
// If s[i] == '*' there can be
// 9 possible values of *
if (s[i] == '*') {
dp[i + 1] = 9 * dp[i];
// If previous character is 1
// then words that can be formed
// are K(11), L(12), M(13), N(14)
// O(15), P(16), Q(17), R(18), S(19)
if (s[i - 1] == '1')
dp[i + 1]
= (dp[i + 1] + 9 * dp[i - 1]) % M;
// If previous character is 2
// then the words that can be formed
// are U(21), V(22), W(23), X(24)Y(25), Z(26)
else if (s[i - 1] == '2')
dp[i + 1]
= (dp[i + 1] + 6 * dp[i - 1]) % M;
// If the previous digit is * then
// all 15 2- digit characters can be
// formed
else if (s[i - 1] == '*')
dp[i + 1]
= (dp[i + 1] + 15 * dp[i - 1]) % M;
}
else {
// taking the value from previous step
dp[i + 1] = s[i] != '0' ? dp[i] : 0;
// If previous character is 1 then
// the i-1th character and ith
// character can be decoded in
// a single character therefore,
// adding dp[i-1].
if (s[i - 1] == '1')
dp[i + 1]
= (dp[i + 1] + dp[i - 1])
% M;
// If previous character is 2
// and ith character is less than
// 6
// then the i-1th character and
// ith character can be decoded in
// a single character therefore,
// adding dp[i-1].
else if (s[i - 1] == '2'
&& s[i] <= '6')
dp[i + 1]
= (dp[i + 1] + dp[i - 1]) % M;
// If previous character is * then
// it will contain the above 2 cases
else if (s[i - 1] == '*')
dp[i + 1]
= (dp[i + 1]
+ (s[i] <= '6' ? 2 : 1)
* dp[i - 1])
% M;
}
}
return dp[(int)s.size()];
}
int main()
{
string s = "12";
cout<<waysOfDecoding(s);
return 0;
}
// This code is contributed by mohit kumar 29.
Java
// Java program for the above approach
import java.io.*;
class GFG {
static int M = 1000000007;
static int waysOfDecoding(String s)
{
long[] dp = new long[s.length() + 1];
dp[0] = 1;
// check the first character of the string
// if it is '*' then 9 ways
dp[1] = s.charAt(0) == '*'
? 9
: s.charAt(0) == '0' ? 0 : 1;
// traverse the string
for (int i = 1; i < s.length(); i++) {
// If s[i] == '*' there can be
// 9 possible values of *
if (s.charAt(i) == '*') {
dp[i + 1] = 9 * dp[i];
// If previous character is 1
// then words that can be formed
// are K(11), L(12), M(13), N(14)
// O(15), P(16), Q(17), R(18), S(19)
if (s.charAt(i - 1) == '1')
dp[i + 1]
= (dp[i + 1] + 9 * dp[i - 1]) % M;
// If previous character is 2
// then the words that can be formed
// are U(21), V(22), W(23), X(24)Y(25), Z(26)
else if (s.charAt(i - 1) == '2')
dp[i + 1]
= (dp[i + 1] + 6 * dp[i - 1]) % M;
// If the previous digit is * then
// all 15 2- digit characters can be
// formed
else if (s.charAt(i - 1) == '*')
dp[i + 1]
= (dp[i + 1] + 15 * dp[i - 1]) % M;
}
else {
// taking the value from previous step
dp[i + 1] = s.charAt(i) != '0' ? dp[i] : 0;
// If previous character is 1 then
// the i-1th character and ith
// character can be decoded in
// a single character therefore,
// adding dp[i-1].
if (s.charAt(i - 1) == '1')
dp[i + 1]
= (dp[i + 1] + dp[i - 1])
% M;
// If previous character is 2
// and ith character is less than
// 6
// then the i-1th character and
// ith character can be decoded in
// a single character therefore,
// adding dp[i-1].
else if (s.charAt(i - 1) == '2'
&& s.charAt(i) <= '6')
dp[i + 1]
= (dp[i + 1] + dp[i - 1]) % M;
// If previous character is * then
// it will contain the above 2 cases
else if (s.charAt(i - 1) == '*')
dp[i + 1]
= (dp[i + 1]
+ (s.charAt(i) <= '6' ? 2 : 1)
* dp[i - 1])
% M;
}
}
return (int)dp[s.length()];
}
public static void main(String[] args)
{
String s = "12";
System.out.println(waysOfDecoding(s));
}
}
Python3
# Python program for the above approach
M = 1000000007
def waysOfDecoding(s):
dp = [0]*(len(s)+1)
dp[0] = 1
# check the first character of the string
# if it is '*' then 9 ways
if s[0] == '*':
dp[1] = 9
elif s[0] == '0':
dp[1] = 0
else:
dp[1] = 1
# traverse the string
for i in range(len(s)):
# If s[i] == '*' there can be
# 9 possible values of *
if (s[i] == '*'):
dp[i + 1] = 9 * dp[i]
# If previous character is 1
# then words that can be formed
# are K(11), L(12), M(13), N(14)
# O(15), P(16), Q(17), R(18), S(19)
if (s[i - 1] == '1'):
dp[i + 1] = (dp[i + 1] + 9 * dp[i - 1]) % M
# If previous character is 2
# then the words that can be formed
# are U(21), V(22), W(23), X(24)Y(25), Z(26)
elif (s[i - 1] == '2'):
dp[i + 1] = (dp[i + 1] + 6 * dp[i - 1]) % M
# If the previous digit is * then
# all 15 2- digit characters can be
# formed
elif (s[i - 1] == '*'):
dp[i + 1] = (dp[i + 1] + 15 * dp[i - 1]) % M
else:
# taking the value from previous step
if s[i] != '0':
dp[i+1] = dp[i]
else:
dp[i+1] = 0
# If previous character is 1 then
# the i-1th character and ith
# character can be decoded in
# a single character therefore,
# adding dp[i-1].
if (s[i - 1] == '1'):
dp[i + 1] = (dp[i + 1] + dp[i - 1]) % M
# If previous character is 2
# and ith character is less than
# 6
# then the i-1th character and
# ith character can be decoded in
# a single character therefore,
# adding dp[i-1].
elif (s[i - 1] == '2'
and s[i] <= '6'):
dp[i + 1] = (dp[i + 1] + dp[i - 1]) % M
# If previous character is * then
# it will contain the above 2 cases
elif (s[i - 1] == '*'):
if (s[i] <= '6'):
dp[i + 1] = dp[i + 1] + 2 * dp[i - 1]
else:
dp[i + 1] = dp[i + 1] + 1 * dp[i - 1]
dp[i+1] = dp[i+1] % M
return dp[len(s)]
if __name__ == "__main__":
s = "12"
print(waysOfDecoding(s))
# This code is contributed by ukasp.
C#
// C# program for the above approach
using System;
class GFG{
static int M = 1000000007;
static int waysOfDecoding(String s)
{
long[] dp = new long[s.Length + 1];
dp[0] = 1;
// Check the first character of the string
// if it is '*' then 9 ways
dp[1] = s[0] == '*' ? 9 : s[0] == '0' ? 0 : 1;
// Traverse the string
for(int i = 1; i < s.Length; i++)
{
// If s[i] == '*' there can be
// 9 possible values of *
if (s[i] == '*')
{
dp[i + 1] = 9 * dp[i];
// If previous character is 1
// then words that can be formed
// are K(11), L(12), M(13), N(14)
// O(15), P(16), Q(17), R(18), S(19)
if (s[i - 1] == '1')
dp[i + 1] = (dp[i + 1] + 9 *
dp[i - 1]) % M;
// If previous character is 2
// then the words that can be formed
// are U(21), V(22), W(23), X(24)Y(25),
// Z(26)
else if (s[i - 1] == '2')
dp[i + 1] = (dp[i + 1] + 6 *
dp[i - 1]) % M;
// If the previous digit is * then
// all 15 2- digit characters can be
// formed
else if (s[i - 1] == '*')
dp[i + 1] = (dp[i + 1] + 15 *
dp[i - 1]) % M;
}
else
{
// Taking the value from previous step
dp[i + 1] = s[i] != '0' ? dp[i] : 0;
// If previous character is 1 then
// the i-1th character and ith
// character can be decoded in
// a single character therefore,
// adding dp[i-1].
if (s[i - 1] == '1')
dp[i + 1] = (dp[i + 1] + dp[i - 1]) % M;
// If previous character is 2
// and ith character is less than
// 6
// then the i-1th character and
// ith character can be decoded in
// a single character therefore,
// adding dp[i-1].
else if (s[i - 1] == '2' && s[i] <= '6')
dp[i + 1] = (dp[i + 1] + dp[i - 1]) % M;
// If previous character is * then
// it will contain the above 2 cases
else if (s[i - 1] == '*')
dp[i + 1] = (dp[i + 1] + (s[i] <= '6' ? 2 : 1) *
dp[i - 1]) % M;
}
}
return (int)dp[s.Length];
}
// Driver code
public static void Main()
{
String s = "12";
Console.WriteLine(waysOfDecoding(s));
}
}
// This code is contributed by rishavmahato348
JavaScript
<script>
// Javascript program for the above approach
let M = 1000000007;
function waysOfDecoding(s)
{
let dp = new Array(s.length + 1);
for(let i=0;i<s.length+1;i++)
dp[i] = 0;
dp[0] = 1;
// check the first character of the string
// if it is '*' then 9 ways
dp[1] = s[0] == '*'
? 9
: s[0] == '0' ? 0 : 1;
// traverse the string
for (let i = 1; i < s.length; i++) {
// If s[i] == '*' there can be
// 9 possible values of *
if (s[i] == '*') {
dp[i + 1] = 9 * dp[i];
// If previous character is 1
// then words that can be formed
// are K(11), L(12), M(13), N(14)
// O(15), P(16), Q(17), R(18), S(19)
if (s[i-1] == '1')
dp[i + 1]
= (dp[i + 1] + 9 * dp[i - 1]) % M;
// If previous character is 2
// then the words that can be formed
// are U(21), V(22), W(23), X(24)Y(25), Z(26)
else if (s[i-1] == '2')
dp[i + 1]
= (dp[i + 1] + 6 * dp[i - 1]) % M;
// If the previous digit is * then
// all 15 2- digit characters can be
// formed
else if (s[i-1] == '*')
dp[i + 1]
= (dp[i + 1] + 15 * dp[i - 1]) % M;
}
else {
// taking the value from previous step
dp[i + 1] = s[i] != '0' ? dp[i] : 0;
// If previous character is 1 then
// the i-1th character and ith
// character can be decoded in
// a single character therefore,
// adding dp[i-1].
if (s[i-1] == '1')
dp[i + 1]
= (dp[i + 1] + dp[i - 1])
% M;
// If previous character is 2
// and ith character is less than
// 6
// then the i-1th character and
// ith character can be decoded in
// a single character therefore,
// adding dp[i-1].
else if (s[i-1] == '2'
&& s[i] <= '6')
dp[i + 1]
= (dp[i + 1] + dp[i - 1]) % M;
// If previous character is * then
// it will contain the above 2 cases
else if (s[i-1] == '*')
dp[i + 1]
= (dp[i + 1]
+ (s[i] <= '6' ? 2 : 1)
* dp[i - 1])
% M;
}
}
return dp[s.length];
}
let s = "12";
document.write(waysOfDecoding(s));
// This code is contributed by unknown2108
</script>
Time complexity: O(n)
Auxiliary Space: O(n)
Further optimization of space
If the above code is observed carefully, it is observed that the value of dp[i] is found using dp[i-1] and dp[i-2]. So to optimize the space further, instead of creating an array of dp of length N, we can use three variables - second(stores the value of dp[i]), first(stores the value of dp[i-2]), and temp(stores the value of dp[i-1]). So after finding the value of the second(dp[i]), modify first = temp and temp = second and then calculate the value again of second(dp[i]) using the variable first and temp.
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
int M = 1000000007;
int waysOfDecoding(string s)
{
long first = 1,
second = s[0] == '*' ? 9 : s[0] == '0' ? 0 : 1;
for(int i = 1; i < s.size(); i++)
{
long temp = second;
// If s[i] == '*' there can be
// 9 possible values of *
if (s[i] == '*')
{
second = 9 * second;
// If previous character is 1
// then words that can be formed
// are K(11), L(12), M(13), N(14)
// O(15), P(16), Q(17), R(18), S(19)
if (s[i - 1] == '1')
second = (second + 9 * first) % M;
// If previous character is 2
// then the words that can be formed
// are U(21), V(22), W(23), X(24)Y(25), Z(26)
else if (s[i - 1] == '2')
second = (second + 6 * first) % M;
// If the previous digit is * then
// all 15 2- digit characters can be
// formed
else if (s[i - 1] == '*')
second = (second + 15 * first) % M;
}
// If s[i] != '*'
else
{
second = s[i] != '0' ? second : 0;
// Adding first in second
// if s[i-1]=1
if (s[i - 1] == '1')
second = (second + first) % M;
// Adding first in second if
// s[i-1] == 2 and s[i]<='6'
else if (s[i - 1] == '2' && s[i] <= '6')
second = (second + first) % M;
// If s[i-1] == '*' the union
// of above 2 cases has to be done
else if (s[i - 1] == '*')
second = (second + (s[i] <= '6' ? 2 : 1) *
first) % M;
}
first = temp;
}
return(int)second;
}
// Driver code
int main()
{
string s = "*";
cout << waysOfDecoding(s);
return 0;
}
// This code is contributed by rishavmahato348
Java
// Java program for the above approach
import java.io.*;
class GFG {
static int M = 1000000007;
static int waysOfDecoding(String s)
{
long first = 1, second
= s.charAt(0) == '*'
? 9
: s.charAt(0) == '0' ? 0 : 1;
for (int i = 1; i < s.length(); i++) {
long temp = second;
// If s[i] == '*' there can be
// 9 possible values of *
if (s.charAt(i) == '*') {
second = 9 * second;
// If previous character is 1
// then words that can be formed
// are K(11), L(12), M(13), N(14)
// O(15), P(16), Q(17), R(18), S(19)
if (s.charAt(i - 1) == '1')
second = (second + 9 * first) % M;
// If previous character is 2
// then the words that can be formed
// are U(21), V(22), W(23), X(24)Y(25), Z(26)
else if (s.charAt(i - 1) == '2')
second = (second + 6 * first) % M;
// If the previous digit is * then
// all 15 2- digit characters can be
// formed
else if (s.charAt(i - 1) == '*')
second = (second + 15 * first) % M;
}
// If s[i] != '*'
else {
second = s.charAt(i) != '0' ? second : 0;
// Adding first in second
// if s[i-1]=1
if (s.charAt(i - 1) == '1')
second = (second + first) % M;
// Adding first in second if
// s[i-1] == 2 and s[i]<='6'
else if (s.charAt(i - 1) == '2'
&& s.charAt(i) <= '6')
second = (second + first) % M;
// if s[i-1] == '*' the union
// of above 2 cases has to be done
else if (s.charAt(i - 1) == '*')
second = (second
+ (s.charAt(i) <= '6' ? 2 : 1)
* first)
% M;
}
first = temp;
}
return (int)second;
}
public static void main(String[] args)
{
String s = "*";
System.out.println(waysOfDecoding(s));
}
}
Python3
# Python program for the above approach
M = 1000000007
def waysOfDecoding(s):
first = 1
second = 9 if(s[0] == '*') else(0 if(s[0] == '0') else 1)
for i in range(1,len(s)):
temp = second
# If s[i] == '*' there can be
# 9 possible values of *
if (s[i] == '*'):
second = 9 * second
# If previous character is 1
# then words that can be formed
# are K(11), L(12), M(13), N(14)
# O(15), P(16), Q(17), R(18), S(19)
if (s[i - 1] == '1'):
second = (second + 9 * first) % M
# If previous character is 2
# then the words that can be formed
# are U(21), V(22), W(23), X(24)Y(25), Z(26)
elif (s[i - 1] == '2'):
second = (second + 6 * first) % M
# If the previous digit is * then
# all 15 2- digit characters can be
# formed
elif (s[i - 1] == '*'):
second = (second + 15 * first) % M
# If s[i] != '*'
else:
second = second if(s[i] != '0') else 0
# Adding first in second
# if s[i-1]=1
if (s[i - 1] == '1'):
second = (second + first) % M
# Adding first in second if
# s[i-1] == 2 and s[i]<=l
elif (s[i - 1] == '2' and s[i] <= '6'):
second = (second + first) % M
# if s[i-1] == '*' the union
# of above 2 cases has to be done
elif (s[i - 1] == '*'):
second = (second + (2 if(s[i] <= '6') else 1) * first) % M
first = temp
return second
# Driver Code
s = "*"
print(waysOfDecoding(s))
# This code is contributed by shinjanpatra
C#
// C# program for the above approach
using System;
class GFG{
static int M = 1000000007;
static int waysOfDecoding(string s)
{
long first = 1,
second = s[0] == '*' ? 9 : s[0] == '0' ? 0 : 1;
for(int i = 1; i < s.Length; i++)
{
long temp = second;
// If s[i] == '*' there can be
// 9 possible values of *
if (s[i] == '*')
{
second = 9 * second;
// If previous character is 1
// then words that can be formed
// are K(11), L(12), M(13), N(14)
// O(15), P(16), Q(17), R(18), S(19)
if (s[i - 1] == '1')
second = (second + 9 * first) % M;
// If previous character is 2
// then the words that can be formed
// are U(21), V(22), W(23), X(24)Y(25), Z(26)
else if (s[i - 1] == '2')
second = (second + 6 * first) % M;
// If the previous digit is * then
// all 15 2- digit characters can be
// formed
else if (s[i - 1] == '*')
second = (second + 15 * first) % M;
}
// If s[i] != '*'
else
{
second = s[i] != '0' ? second : 0;
// Adding first in second
// if s[i-1]=1
if (s[i - 1] == '1')
second = (second + first) % M;
// Adding first in second if
// s[i-1] == 2 and s[i]<='6'
else if (s[i - 1] == '2' && s[i] <= '6')
second = (second + first) % M;
// if s[i-1] == '*' the union
// of above 2 cases has to be done
else if (s[i - 1] == '*')
second = (second + (s[i] <= '6' ? 2 : 1) *
first) % M;
}
first = temp;
}
return (int)second;
}
// Driver code
static public void Main()
{
string s = "*";
Console.WriteLine(waysOfDecoding(s));
}
}
// This code is contributed by patel2127
JavaScript
<script>
// JavaScript program for the above approach
let M = 1000000007;
function waysOfDecoding(s)
{
let first = 1,
second = s[0] == '*' ? 9 : s[0] == '0' ? 0 : 1;
for(let i = 1; i < s.length; i++)
{
let temp = second;
// If s[i] == '*' there can be
// 9 possible values of *
if (s[i] == '*')
{
second = 9 * second;
// If previous character is 1
// then words that can be formed
// are K(11), L(12), M(13), N(14)
// O(15), P(16), Q(17), R(18), S(19)
if (s[i - 1] == '1')
second = (second + 9 * first) % M;
// If previous character is 2
// then the words that can be formed
// are U(21), V(22), W(23), X(24)Y(25), Z(26)
else if (s[i - 1] == '2')
second = (second + 6 * first) % M;
// If the previous digit is * then
// all 15 2- digit characters can be
// formed
else if (s[i - 1] == '*')
second = (second + 15 * first) % M;
}
// If s[i] != '*'
else
{
second = s[i] != '0' ? second : 0;
// Adding first in second
// if s[i-1]=1
if (s[i - 1] == '1')
second = (second + first) % M;
// Adding first in second if
// s[i-1] == 2 and s[i]<='6'
else if (s[i - 1] == '2' && s[i] <= '6')
second = (second + first) % M;
// if s[i-1] == '*' the union
// of above 2 cases has to be done
else if (s[i - 1] == '*')
second = (second + (s[i] <= '6' ? 2 : 1) *
first) % M;
}
first = temp;
}
return second;
}
// Driver Code
let s = "*";
document.write(waysOfDecoding(s));
// This code is contributed by code_hunt
</script>
Time complexity: O(n)
Auxiliary Space: O(1)
Similar Reads
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
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
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 the sum of count of distinct characters present in all Substrings Given a string S consisting of lowercase English letters of size N where (1 <= N <= 105), the task is to print the sum of the count of distinct characters N where (1 <= N <= 105)in all the substrings. Examples: Input: str = "abbca"Output: 28Explanation: The following are the substrings o
8 min read
Queries to count frequencies of a given character in a given range of indices Given a string S of length N and an array Q[][] of queries in the form {l, r, y}. For each query, the task is to print the number of characters y present in the range [l, r]. Examples: Input: S = "aabv", Q[][] = {{0, 3, 'a'}, {1, 2, 'b'}}Output: 2 1Explanation:Query 1: Number of character 'a' presen
7 min read
Count of substrings which contains a given character K times Given a string consisting of numerical alphabets, a character C and an integer K, the task is to find the number of possible substrings which contains the character C exactly K times. Examples: Input : str = "212", c = '2', k = 1 Output : 4 Possible substrings are {"2", "21", "12", "2"} that contain
9 min read
Count of Distinct strings possible by inserting K characters in the original string Given a string S and an integer K, the task is to find the total number of strings that can be formed by inserting exactly K characters at any position of the string S. Since the answer can be large, print it modulo 109+7.Examples: Input: S = "a" K = 1 Output: 51 Explanation: Since any of the 26 cha
12 min read
Count substrings made up of a single distinct character Given a string S of length N, the task is to count the number of substrings made up of a single distinct character.Note: For the repetitive occurrences of the same substring, count all repetitions. Examples: Input: str = "geeksforgeeks"Output: 15Explanation: All substrings made up of a single distin
5 min read
Queries to find the count of characters preceding the given location Given a string S of length N containing only lowercase letters. Also, given Q queries where each query consists of an integer P such that 1? P ? N. The task is to find the count of occurrences of the same letter preceding the given location P. Examples : Input: S = "abacsddaa", Q[] = {9, 3} Output:
8 min read
Count of sub-strings that do not consist of the given character Given a string str and a character c. The task is to find the number of sub-strings that do not consist of the character c. Examples: Input: str = "baa", c = 'b' Output: 3 The sub-strings are "a", "a" and "aa" Input: str = "ababaa", C = 'b' Output: 5 Approach: Initially take a counter that counts th
12 min read