“常用公式”在线计算,“设计手册”在线查询
(all valid rearrangements of the letters that form validEnglish words) You are allowed to pre-compute what ever you want to and store what ever you optionally pre‐compute on disk.
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 分享淘帖 支持支持 反对反对

共 1 个关于本帖的回复 最后回复于 2013-7-30 14:51

沙发
淡写轻描 新来的 发表于 2013-7-30 14:51:46 | 只看该作者
研发埠培训中心
Answer: We want to use a hashtable!If your interviewer really hates hashtables(which they sometimes do for some reason),you can use a tree instead.But let'sassume you can use a hashtable.Then for the pre‐computing step,go through eachword in the dictionary,sort the letters of the word in alphabetical order(so"hacking" would become "acghikn")and add the sorted letters as a key in the tableand the original word as one of the values in a list of values for that key.Forexample,the entry for "opst" would be the list["opts","post","stop","pots","tops","spot"].Then,when ever you get a string,you simply sort the letters of the string and look up the value in the hashtable.The running time is O(nlogn) for sorting the string(which is relatively small)and approximately O(1) for the look up in the hashtable.There are several other possible answers to this question,but we feel that theanswer above is considered an optimal solution.
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注我们

360网站安全检测平台