📜  计算用重复数字拼写数字的方法

📅  最后修改于: 2021-04-26 10:03:42             🧑  作者: Mango

给定一个包含数字位数的字符串。该数字中可能包含许多相同的连续数字。任务是计算拼写数字的方法数量。
例如,考虑8884441100,可以简单地将其拼写为三重八,三重四,双二和双零。一个也可以拼成双八,八,四,双四,二,二,双零。

例子 :

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

这是排列和组合的简单问题。如果我们以问题11112中给出的示例测试用例为例,答案取决于1111的可能组合数。“ 1111”的组合数为2 ^ 3 =8。因为我们的组合取决于我们是否选择a特别是1,对于“ 2”,只有一种可能性2 ^ 0 = 1,因此对“ 11112”的答案将是8 * 1 = 8。

因此,方法是对字符串的特定连续数字进行计数,然后将2 ^(count-1)与先前的结果相乘。

C++
// C++ program to count number of ways we
// can spell a number
#include
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


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


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
 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.
?>


Javascript


输出 :

8