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

前端 未结 30 2193
逝去的感伤
逝去的感伤 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:35

    You find source (Scala) here and an working applet here.

    Since 0 and 1 aren't matched to characters, they build natural breakpoints in numbers. But they don't occur in every number (except 0 at the beginning). Longer numbers like +49567892345 from 9 digits starting, can lead to OutOfMemoryErrors. So it would be better to split a number into groups like

    • 01723 5864
    • 0172 35864

    to see, if you can make sense from the shorter parts. I wrote such a program, and tested some numbers from my friends, but found rarely combinations of shorter words, which could be checked in a dictionary for matching, not to mention single, long words.

    So my decision was to only support searching, no full automation, by displaying possible combinations, encouraging splitting the number by hand, maybe multiple time.

    So I found +-RAD JUNG (+-bycicle boy).

    If you accept misspellings, abbreviations, foreign words, numbers as words, numbers in words, and names, your chance to find a solution is much better, than without fiddling around.

    246848 => 2hot4u (too hot for you) 
    466368 => goodn8 (good night) 
    1325   => 1FCK   (Football club)
    53517  => JDK17  (Java Developer Kit)
    

    are things a human might observe - to make an algorithm find such things is rather hard.

提交回复
热议问题