How can I print out all possible letter combinations a given phone number can represent?

前端 未结 30 2277
逝去的感伤
逝去的感伤 2020-12-22 18:01

I just tried for my first programming interview and one of the questions was to write a program that given a 7 digit telephone number, could print all possible combinations

30条回答
  •  不知归路
    2020-12-22 18:51

    Use a list L where L[i] = the symbols that digit i can represent.

    L[1] = @,.,! (for example) L[2] = a,b,c

    Etc.

    Then you can do something like this (pseudo-C):

    void f(int k, int st[])
    {
      if ( k > numberOfDigits )
      {
        print contents of st[];
        return;
      }
    
      for each character c in L[Digit At Position k]
      {
        st[k] = c;
        f(k + 1, st);
      }
    }
    

    Assuming each list contains 3 characters, we have 3^7 possibilities for 7 digits and 3^12 for 12, which isn't that many. If you need all combinations, I don't see a much better way. You can avoid recursion and whatnot, but you're not going to get something a lot faster than this no matter what.

提交回复
热议问题