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.
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.
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 email@example.com. 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.
- Remove repeated digits in a given number
- Recursive sum of digits of a number formed by repeated appends
- Find M-th number whose repeated sum of digits of a number is N
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Count number of ways to get Odd Sum
- Count number of ways to divide a number in 4 parts
- Count ways to express even number ‘n’ as sum of even integers
- Count number of ways to arrange first N numbers
- Count number of ways to reach a given score in a Matrix
- Count number of ways to divide an array into two halves with same sum
- Count ways to express a number as sum of consecutive numbers
- Find count of digits in a number that divide the number
- Count number of digits after decimal on dividing a number
- Count total number of digits from 1 to n