Count ways to spell a number with repeated digits
Given a string that contains digits of a number. The number may contain many same continuous digits in it. The task is to count number of ways to spell the number.
For example, consider 8884441100, one can spell it simply as triple eight triple four double two and double zero. One can also spell as double eight, eight, four, double four, two, two, double zero.
Examples :
Input : num = 100
Output : 2
The number 100 has only 2 possibilities,
1) one zero zero
2) one double zero.
Input : num = 11112
Output: 8
1 1 1 1 2, 11 1 1 2, 1 1 11 2, 1 11 1 2
11 11 2, 1 111 2, 111 1 2, 1111 2
Input : num = 8884441100
Output: 64
Input : num = 12345
Output: 1
Input : num = 11111
Output: 16
This is a simple problem of permutation and combination. If we take example test case given in the question, 11112. The answer depends on the number of possible substrings of 1111. The number of possible substrings of “1111” is 2^3 = 8 because it is the number of combinations of 4 – 1 = 3 separators ‘|’ between two characters of the string (digits of number represented by the string) : “1|1|1|1”. As our combinations will depend on whether we choose a particular 1 and for “2” there will be only one possibility 2^0 = 1, so answer for “11112” will be 8*1 = 8.
So, the approach is to count the particular continuous digit in string and multiply 2^(count-1) with previous result.
C++
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll spellsCount(string num)
{
int n = num.length();
ll result = 1;
for ( int i=0; i<n; i++)
{
int count = 1;
while (i < n-1 && num[i+1] == num[i])
{
count++;
i++;
}
result = result * pow (2, count-1);
}
return result;
}
int main()
{
string num = "11112" ;
cout << spellsCount(num);
return 0;
}
|
Java
import java.io.*;
class GFG {
static long spellsCount(String num)
{
int n = num.length();
long result = 1 ;
for ( int i = 0 ; i < n; i++) {
int count = 1 ;
while (i < n - 1 && num.charAt(i + 1 )
== num.charAt(i)) {
count++;
i++;
}
result = result *
( long )Math.pow( 2 , count - 1 );
}
return result;
}
public static void main(String[] args)
{
String num = "11112" ;
System.out.print(spellsCount(num));
}
}
|
Python3
def spellsCount(num):
n = len (num);
result = 1 ;
i = 0 ;
while (i<n):
count = 1 ;
while (i < n - 1 and
num[i + 1 ] = = num[i]):
count + = 1 ;
i + = 1 ;
result = result * int ( pow ( 2 , count - 1 ));
i + = 1 ;
return result;
num = "11112" ;
print (spellsCount(num));
|
C#
using System;
class GFG {
static long spellsCount(String num)
{
int n = num.Length;
long result = 1;
for ( int i = 0; i < n; i++)
{
int count = 1;
while (i < n - 1 && num[i + 1]
== num[i])
{
count++;
i++;
}
result = result *
( long )Math.Pow(2, count - 1);
}
return result;
}
public static void Main()
{
String num = "11112" ;
Console.Write(spellsCount(num));
}
}
|
PHP
<?php
function spellsCount( $num )
{
$n = strlen ( $num );
$result = 1;
for ( $i = 0; $i < $n ; $i ++)
{
$count = 1;
while ( $i < $n - 1 &&
$num [ $i + 1] == $num [ $i ])
{
$count ++;
$i ++;
}
$result = $result *
pow(2, $count - 1);
}
return $result ;
}
$num = "11112" ;
echo spellsCount( $num );
?>
|
Javascript
<script>
function spellsCount(num)
{
let n = num.length;
let result = 1;
for (let i = 0; i < n; i++)
{
let count = 1;
while (i < n - 1 &&
num[i + 1] == num[i])
{
count++;
i++;
}
result = result *
Math.pow(2, count - 1);
}
return result;
}
let num = "11112" ;
document.write(spellsCount(num));
</script>
|
Time Complexity: O(n*log(n))
Auxiliary Space: O(1)
If you have another approach to solve this problem then please share.
Last Updated :
14 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...