Počítejte způsoby, jak napsat číslo s opakovanými číslicemi

Počítejte způsoby, jak napsat číslo s opakovanými číslicemi
Zkuste to na GfG Practice #practiceLinkDiv { display: none !important; }

Je dán řetězec, který obsahuje číslice čísla. Číslo může obsahovat mnoho stejných souvislých číslic. Úkolem je spočítat počet způsobů, jak číslo napsat. 
Uvažujme například 8884441100, dá se to napsat jednoduše jako trojitá osm trojitá čtyři dvojitá dvě a dvojitá nula. Dá se také hláskovat jako dvojitá osm osm čtyři dvojitá čtyři dvě dvě dvojitá nula. 

Příklady:   

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 
Recommended Practice Vyhláskujte číslo Zkuste to!

Toto je jednoduchý problém permutace a kombinace. Vezmeme-li příklad testovací případ uvedený v otázce 11112. Odpověď závisí na počtu možných podřetězců 1111. Počet možných podřetězců '1111' je 2^3 = 8, protože jde o počet kombinací 4 - 1 =  3 oddělovačů '|' mezi dvěma znaky řetězce (číslice čísla reprezentované řetězcem): '1|1|1|1'. Protože naše kombinace budou záviset na tom, zda zvolíme konkrétní 1 a pro '2' bude pouze jedna možnost 2^0 = 1, takže odpověď pro '11112' bude 8*1 = 8. 

Přístup je tedy spočítat konkrétní spojitou číslici v řetězci a vynásobit 2^(počet-1) s předchozím výsledkem. 

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   <  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   import     java.io.*  ;   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 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.    ?>   
JavaScript
    <  script  >   // Javascript 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  )   {      let     n     =     num  .  length  ;      // Final count of total possible      // spells      let     result     =     1  ;      // Iterate through complete number      for     (  let     i     =     0  ;     i      <     n  ;     i  ++  )      {          // Count contiguous frequency of       // particular digit num[i]      let     count     =     1  ;          while     (  i      <     n     -     1     &&         num  [  i     +     1  ]     ==     num  [  i  ])      {      count  ++  ;      i  ++  ;      }      // Compute 2^(count-1) and multiply       // with result      result     =     result     *         Math  .  pow  (  2       count     -     1  );      }      return     result  ;   }       // Driver code   let     num     =     '11112'  ;   document  .  write  (  spellsCount  (  num  ));   // This code is contributed by code_hunt        <  /script>   

Výstup
8 

Časová náročnost: O(n*log(n))
Pomocný prostor: O(1)

Pokud máte jiný přístup k vyřešení tohoto problému, sdílejte prosím.
 

Vytvořit kvíz