数字をダイヤルするために使用できる文字列のすべての組み合わせ

可能な限り番号を印刷してください 組み合わせ 次の仕様を備えた電話で指定された番号をダイヤルするために使用できる文字列の。指定された携帯電話では、dまたはeまたはfを使用してaまたはbまたはc 3を使用して2ダイヤルできます..................... 8は、wまたはxまたはyまたはyを使用して1 0を使用してtまたはuまたはv 9を使用して0を使用します。

アイデアは、ハッシュマップでマッピングマッピングに桁を保存することです。マップには、使用できるすべての文字が桁をダイヤルします。可能なすべての文字を現在の数字に配置し、残りの数字に再発します。 

アルゴリズム:

  • 0から9の数字としてキーを持つハッシュマップ、各数字に関連付けられた文字のセットとして値を作成します。
  • 4つの引数を取得する再帰関数プリントストリングを定義します。
    a。 PHNO-入力電話番号
    b。 i-処理中の現在の数字のインデックス
    c。 HM-文字セットへの数字のハッシュマップ
    d。 str-これまでに生成された文字列の文字列
  • Printstringsの内部​​関数:
    a。電話番号の終わりに達したかどうかを確認してください。はいの場合、生成された文字列を印刷して返します。
    b。ハッシュマップから現在の数字に関連付けられた文字のセットを取得します。
    c。セット内の各文字を反復し、
           私。文字列strに文字を追加します。
           ii。次の数字のprintStrings関数を再帰的に呼び出します。
          iii。文字列strから最後の文字を削除します。
  • 1つの引数を取る関数printStringFornumberを定義します。
    a。 PHNO-入力電話番号
  • PrintStringFornumber関数の内部では、引数PHNO 0 HMと空の文字列を使用して、PrintStrings関数を呼び出します。

以下は、このアイデアのJavaの実装です。 

実装:

C++
   // C++ program for the above approach   #include          #include         using     namespace     std  ;   void     printStrings  (  string     phNo       int     i        unordered_map   <  char       string  >     hm        string     str  )   {      if     (  i     ==     phNo  .  length  ())      {      cout      < <     str      < <     ' '  ;      return  ;      }      string     s     =     hm  [  phNo  [  i  ]];      for     (  int     j     =     0  ;     j      <     s  .  length  ();     j  ++  )      {      str  .  push_back  (  s  [  j  ]);      printStrings  (  phNo       i  +  1       hm       str  );      str  .  pop_back  ();      }   }   void     printStringForNumber  (  string     phNo  )   {      unordered_map   <  char       string  >     hm     =     {      {  '2'       'ABC'  }      {  '3'       'DEF'  }      {  '4'       'GHI'  }      {  '5'       'JKL'  }      {  '6'       'MNO'  }      {  '7'       'PQRS'  }      {  '8'       'TUV'  }      {  '9'       'WXYZ'  }      {  '1'       '1'  }      {  '0'       '0'  }      };      string     str  ;      printStrings  (  phNo       0       hm       str  );   }   int     main  ()   {      printStringForNumber  (  '23'  );      return     0  ;   }   // This code is contributed by codebraxnzt   
Java
   // Java program to print all possible key strings   // that can be used to dial a phone number.   import     java.util.HashMap  ;   class   ConvertToString   {      // A Recursive function to print all combinations      // that can be used to dial a given number.      // phNo ==> Given Phone Number      // i ==> Current digit of phNo to be processed      // hm ==> Stores characters that can be used to      // to dial a digit.      // str ==> Current output string      static     void     printStrings  (  String     phNo       int     i        HashMap   <  Character       String  >     hm        StringBuilder     str  )      {      // If all digits are processed print output      // string      if     (  i     ==     phNo  .  length  ())      {      System  .  out  .  print  (  str     +     ' '  );      return  ;      }      // Get current digit of phNo and recur for all      // characters that can be used to dial it.      String     s     =     hm  .  get  (  phNo  .  charAt  (  i  ));      for     (  int     j     =     0  ;     j      <     s  .  length  ();     j  ++  )      {      str  .  append  (  s  .  charAt  (  j  ));      printStrings  (  phNo       i  +  1       hm       str  );      str  .  deleteCharAt  (  str  .  length  ()  -  1  );      }      }      // Prints all possible combinations of strings that      // can be used to dial c[].      static     void     printStringForNumber  (  String     phNo  )      {      // Create a HashMap      HashMap   <  Character       String  >     hm     =      new     HashMap   <  Character       String  >  ();      // For every digit store characters that can      // be used to dial it.      hm  .  put  (  '2'       'ABC'  );      hm  .  put  (  '3'       'DEF'  );      hm  .  put  (  '4'       'GHI'  );      hm  .  put  (  '5'       'JKL'  );      hm  .  put  (  '6'       'MNO'  );      hm  .  put  (  '7'       'PQRS'  );      hm  .  put  (  '8'       'TUV'  );      hm  .  put  (  '9'       'WXYZ'  );      hm  .  put  (  '1'       '1'  );      hm  .  put  (  '0'       '0'  );      // Create a string to store a particular output      // string      StringBuilder     str     =     new     StringBuilder  ();      // Call recursive function      printStrings  (  phNo       0       hm       str  );      }      // Driver code to test above methods      public     static     void     main  (  String     args  []  )      {      // Prints      printStringForNumber  (  '23'  );      }   }   
Python
   def   print_strings  (  ph_no     i     hm     s  ):   if   i   ==   len  (  ph_no  ):   print  (  s     end  =  ' '  )   return   for   c   in   hm  [  ph_no  [  i  ]]:   print_strings  (  ph_no     i  +  1     hm     s  +  c  )   def   print_string_for_number  (  ph_no  ):   hm   =   {   '2'  :   'ABC'     '3'  :   'DEF'     '4'  :   'GHI'     '5'  :   'JKL'     '6'  :   'MNO'     '7'  :   'PQRS'     '8'  :   'TUV'     '9'  :   'WXYZ'     '1'  :   '1'     '0'  :   '0'   }   s   =   ''   print_strings  (  ph_no     0     hm     s  )   print_string_for_number  (  '23'  )   
C#
   using     System  ;   using     System.Collections.Generic  ;   class     Program     {      static     void     printStrings  (  string     phNo       int     i        Dictionary   <  char       string  >     hm        string     str  )      {      if     (  i     ==     phNo  .  Length  )      {      Console  .  Write  (  str     +     ' '  );      return  ;      }      string     s     =     hm  [  phNo  [  i  ]];      for     (  int     j     =     0  ;     j      <     s  .  Length  ;     j  ++  )      {      str     +=     s  [  j  ];      printStrings  (  phNo       i  +  1       hm       str  );      str     =     str  .  Remove  (  str  .  Length  -  1  );      }      }      static     void     printStringForNumber  (  string     phNo  )      {      Dictionary   <  char       string  >     hm     =     new     Dictionary   <  char       string  >      {      {  '2'       'ABC'  }      {  '3'       'DEF'  }      {  '4'       'GHI'  }      {  '5'       'JKL'  }      {  '6'       'MNO'  }      {  '7'       'PQRS'  }      {  '8'       'TUV'  }      {  '9'       'WXYZ'  }      {  '1'       '1'  }      {  '0'       '0'  }      };      string     str     =     ''  ;      printStrings  (  phNo       0       hm       str  );      }      static     void     Main  (  string  []     args  )     {      printStringForNumber  (  '23'  );      }   }   
JavaScript
   function     printStrings  (  phNo       i       hm       s  )     {      if     (  i     ===     phNo  .  length  )     {      console  .  log  (  s     +     ' '  );      return  ;      }      for     (  let     j     =     0  ;     j      <     hm  [  phNo  [  i  ]].  length  ;     j  ++  )     {      s     +=     hm  [  phNo  [  i  ]][  j  ];      printStrings  (  phNo       i  +  1       hm       s  );      s     =     s  .  slice  (  0       -  1  );      }   }   function     printStringForNumber  (  phNo  )     {      let     hm     =     {      '2'  :     'ABC'        '3'  :     'DEF'        '4'  :     'GHI'        '5'  :     'JKL'        '6'  :     'MNO'        '7'  :     'PQRS'        '8'  :     'TUV'        '9'  :     'WXYZ'        '1'  :     '1'        '0'  :     '0'      };      let     s     =     ''  ;      printStrings  (  phNo       0       hm       s  );   }   printStringForNumber  (  '23'  );   

出力
AD AE AF BD BE BF CD CE CF  

時間の複雑さ:o(2^n)  ここでnは文字列の長さです 

補助スペース:o(n)