【问题标题】:Return a Map of words which can be formed by using other words which exist in the same list返回可以通过使用同一列表中存在的其他单词形成的单词映射
【发布时间】:2021-07-30 11:28:00
【问题描述】:

给定一个单词列表,返回一个单词 Map,该 Map 可以通过使用同一列表中存在的其他单词形成。

 input = [“happy”, “rise”, “for”, “set”, “sunrise”, “su”, “nset”, “sunset”, “mind”, “happymind”, “n”, “rise”, “happysunrise”]

 output = {
  “happymind” : [[“happy”, “mind]],
  “sunrise” : [[“su”, “n”, “rise”], [“sun”, “rise”]],
  “sunset” : [[“sun”, “set”], [“su”, “n”, “set]],  
  “happysunrise” : [[“happy”, “sunrise”], [“happy”, “sun”, “rise”], [“happy”, “su”, “n”, 
   “rise”]]
 }

【问题讨论】:

  • 在给定的输入单词列表中没有单词“sun”,那么它如何出现在输出映射中,其中键是“sunrise”、“sunset”和“happysunrise”?跨度>

标签: string dictionary hashmap


【解决方案1】:

我已经尝试过尝试这些词。但无法达到解决方案。然后我通过制作字典中每个单词的计数图来想到另一种解决方案。 然后对于每个像“快乐”这样的词,通过遍历每个字母来在字典中查找

"happy"
"h" + "appy"
"ha" + "ppy"
"hap" + "py"
"happ" + "y"

但是在这个解决方案中,我在那些中间有一个字母的单词中失败了。喜欢

"sunrise" n is coming in between.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-28
    • 1970-01-01
    • 2021-07-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多