惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Secure Thoughts
Security Latest
Security Latest
Simon Willison's Weblog
Simon Willison's Weblog
O
OpenAI News
GbyAI
GbyAI
L
LINUX DO - 最新话题
A
Arctic Wolf
T
Tor Project blog
G
GRAHAM CLULEY
I
InfoQ
博客园_首页
IT之家
IT之家
The Register - Security
The Register - Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
The GitHub Blog
The GitHub Blog
Blog — PlanetScale
Blog — PlanetScale
N
Netflix TechBlog - Medium
K
Kaspersky official blog
博客园 - 三生石上(FineUI控件)
S
SegmentFault 最新的问题
U
Unit 42
PCI Perspectives
PCI Perspectives
量子位
P
Palo Alto Networks Blog
S
Securelist
T
Troy Hunt's Blog
博客园 - 【当耐特】
Recorded Future
Recorded Future
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
S
Security Affairs
Engineering at Meta
Engineering at Meta
T
The Blog of Author Tim Ferriss
博客园 - 聂微东
罗磊的独立博客
N
News and Events Feed by Topic
人人都是产品经理
人人都是产品经理
B
Blog RSS Feed
NISL@THU
NISL@THU
C
Cisco Blogs
T
Threatpost
有赞技术团队
有赞技术团队
Forbes - Security
Forbes - Security
Hugging Face - Blog
Hugging Face - Blog
Last Week in AI
Last Week in AI
T
The Exploit Database - CXSecurity.com
Cloudbric
Cloudbric
Cyberwarzone
Cyberwarzone
Google DeepMind News
Google DeepMind News
C
Cyber Attacks, Cyber Crime and Cyber Security

博客园 - 司徒正美

leetcode 91. Decode Ways leetcode 1214 Two Sum BSTs leetcode 213 House Robber II leetcode 198 House Robber I leetcode 986. Interval List Intersections leetcode 869. Reordered Power of 2 leetcode 925. Long Pressed Name leetcode 457. Circular Array Loop leetcode 1093. Statistics from a Large Sample leetcode 881. Boats to Save People leetcode 977. Squares of a Sorted Array leetcode 844. Backspace String Compare leetcode 1032. Stream of Characters leetcode 1023. Camelcase Matching leetcode 745 Prefix and Suffix Search leetcode 692. Top K Frequent Words leetcode 677. Map Sum Pairs leetcode 676. Implement Magic Dictionary leetcode 648. Replace Words
leetcode 720. Longest Word in Dictionary
司徒正美 · 2019-12-28 · via 博客园 - 司徒正美

使用前缀树

 function Node() {
      this.word = ''
      this.children = {}
    }
    class Trie {
      constructor() {
        this.root = new Node()
      }
      addWord(word) {
        var node = this.root;
        for (let next of word) {

          if (!node.children[next]) {
            node.children[next] = new Node()
          }
          node = node.children[next]
        }
        node.word = word;
      }
      search(word) {
        var node = this.root;
        for (let c of word) {
          if (!node.children[c] || !node.children[c].word) {
            return false
          }
          node = node.children[c]
        }
        return true
      }
    }


    var longestWord = function (words, k) {
      if (!words.length) {
        return ''
      }
      var tire = new Trie;
      for (let word of words) {
        tire.addWord(word)
      }
      var ret = ''
      for (let word of words) {
        if (tire.search(word)) {
          if (word.length > ret.length) {
            ret = word;//更新最长单词
          } else if (word.length === ret.length && word.localeCompare(ret) < 0) {
            ret = word
          }
        } else { //过滤掉单词的前缀部分
          //  console.log("----", word) //不懂
        }
      }
      return ret;
    };

   console.log(longestWord(["ogz", "eyj", "e", "ey",
      "hmn", "v", "hm", "ogznkb", "ogzn", "hmnm",
      "eyjuo", "vuq", "ogznk", "og", "eyjuoi", "d"]
    ))

    console.log(longestWord(["w", "wo", "wor", "worl", "world"]))
    console.log(longestWord(["a", "banana", "app", "appl", "ap", "apply", "apple"]))