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 combinations of 1111. The number of combinations of “1111” is 2^3 = 8. 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++
// C++ program to count number of ways we // can spell a number #include<bits/stdc++.h> using namespace std; typedef long long int ll; // Function to calculate all possible spells of // a number with repeated digits // num --> string which is favourite number ll spellsCount(string num) { int n = num.length(); // final count of total possible spells ll result = 1; // iterate through complete number for (int i=0; i<n; i++) { // count contiguous frequency of particular // digit num[i] int count = 1; while (i < n-1 && num[i+1] == num[i]) { count++; i++; } // Compute 2^(count-1) and multiply with result result = result * pow(2, count-1); } return result; } // Driver program to run the case int main() { string num = "11112"; cout << spellsCount(num); return 0; } |
Java
// Java program to count number of ways we // can spell a number class GFG { // Function to calculate all possible // spells of a number with repeated digits // num --> string which is favourite number static long spellsCount(String num) { int n = num.length(); // final count of total possible spells long result = 1; // iterate through complete number for (int i = 0; i < n; i++) { // count contiguous frequency of // particular digit num[i] int count = 1; while (i < n - 1 && num.charAt(i + 1) == num.charAt(i)) { count++; i++; } // Compute 2^(count-1) and multiply // with result result = result * (long)Math.pow(2, count - 1); } return result; } public static void main(String[] args) { String num = "11112"; System.out.print(spellsCount(num)); } } // This code is contributed by Anant Agarwal. |
Python3
# Python3 program to count number of # ways we can spell a number # Function to calculate all possible # spells of a number with repeated # digits num --> string which is # favourite number def spellsCount(num): n = len(num); # final count of total # possible spells result = 1; # iterate through complete # number i = 0; while(i<n): # count contiguous frequency # of particular digit num[i] count = 1; while (i < n - 1 and num[i + 1] == num[i]): count += 1; i += 1; # Compute 2^(count-1) and # multiply with result result = result * int(pow(2, count - 1)); i += 1; return result; # Driver Code num = "11112"; print(spellsCount(num)); # This code is contributed # by mits |
C#
// C# program to count number of ways we // can spell a number using System; class GFG { // Function to calculate all possible // spells of a number with repeated // digits num --> string which is // favourite number static long spellsCount(String num) { int n = num.Length; // final count of total possible // spells long result = 1; // iterate through complete number for (int i = 0; i < n; i++) { // count contiguous frequency of // particular digit num[i] int count = 1; while (i < n - 1 && num[i + 1] == num[i]) { count++; i++; } // Compute 2^(count-1) and multiply // with result result = result * (long)Math.Pow(2, count - 1); } return result; } // Driver code public static void Main() { String num = "11112"; Console.Write(spellsCount(num)); } } // This code is contributed by nitin mittal. |
PHP
<?php // PHP program to count // number of ways we // can spell a number // Function to calculate // all possible spells of // a number with repeated // digits num --> string // which is favourite number function spellsCount($num) { $n = strlen($num); // final count of total // possible spells $result = 1; // iterate through // complete number for ($i = 0; $i < $n; $i++) { // count contiguous frequency // of particular digit num[i] $count = 1; while ($i < $n - 1 && $num[$i + 1] == $num[$i]) { $count++; $i++; } // Compute 2^(count-1) and // multiply with result $result = $result * pow(2, $count - 1); } return $result; } // Driver Code $num = "11112"; echo spellsCount($num); // This code is contributed // by nitin mittal. ?> |
Output :
8
If you have another approach to solve this problem then please share.
This article is contributed by Shashank Mishra ( Gullu ). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Count ways to obtain given sum by repeated throws of a dice
- Find M-th number whose repeated sum of digits of a number is N
- Remove repeated digits in a given number
- Recursive sum of digits of a number formed by repeated appends
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Minimum digits to be removed to make either all digits or alternating digits same
- Total numbers with no repeated digits in a range
- Squares of numbers with repeated single digits | Set 1 (3, 6 and 9)
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Find smallest number with given number of digits and sum of digits
- Find the Largest number with given number of digits and sum of digits
- Number of digits in the nth number made of given four digits
- Find smallest number with given number of digits and sum of digits under given constraints
- Number formed by deleting digits such that sum of the digits becomes even and the number odd
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Count of numbers upto N digits formed using digits 0 to K-1 without any adjacent 0s
- Find the count of M character words which have at least one character repeated
- Smallest number with given sum of digits and sum of square of digits
- Minimum number of digits to be removed so that no two consecutive digits are same

