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

推荐订阅源

GbyAI
GbyAI
J
Java Code Geeks
雷峰网
雷峰网
WordPress大学
WordPress大学
宝玉的分享
宝玉的分享
云风的 BLOG
云风的 BLOG
V
Visual Studio Blog
V
Vulnerabilities – Threatpost
S
Securelist
The Hacker News
The Hacker News
The Register - Security
The Register - Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Help Net Security
Help Net Security
G
Google Developers Blog
Hugging Face - Blog
Hugging Face - Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
M
MIT News - Artificial intelligence
AI
AI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
The GitHub Blog
The GitHub Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Schneier on Security
Schneier on Security
N
Netflix TechBlog - Medium
T
The Blog of Author Tim Ferriss
Google DeepMind News
Google DeepMind News
Hacker News - Newest:
Hacker News - Newest: "LLM"
H
Hacker News: Front Page
博客园 - 司徒正美
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
B
Blog
Microsoft Azure Blog
Microsoft Azure Blog
大猫的无限游戏
大猫的无限游戏
Security Latest
Security Latest
Engineering at Meta
Engineering at Meta
N
News and Events Feed by Topic
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
酷 壳 – CoolShell
酷 壳 – CoolShell
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Threat Research - Cisco Blogs
U
Unit 42
V
V2EX
V2EX - 技术
V2EX - 技术
L
LINUX DO - 最新话题
aimingoo的专栏
aimingoo的专栏
Microsoft Security Blog
Microsoft Security Blog
Recorded Future
Recorded Future
P
Privacy & Cybersecurity Law Blog
美团技术团队
小众软件
小众软件
F
Fortinet All Blogs

博客园 - 司徒正美

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 745 Prefix and Suffix Search leetcode 720. Longest Word in Dictionary leetcode 692. Top K Frequent Words leetcode 677. Map Sum Pairs leetcode 676. Implement Magic Dictionary leetcode 648. Replace Words
leetcode 1023. Camelcase Matching
司徒正美 · 2019-12-30 · via 博客园 - 司徒正美

使用前缀树, 多个字符串匹配一个模式

  function Node() {
      this.children = {}
    }
    class Trie {
      constructor() {
        this.root = new Node()
      }
      insert(word) {
        var node = this.root;
        for (let c of word) {
          if (!node.children[c]) {
            node.children[c] = new Node
          }

          node = node.children[c]
        }
        node.word = word;
      }
      search(word) {
        var hash = {}
        innerSearch(this.root, word, 0, true, hash)
        return hash;
      }

    }
    function innerSearch(node, word, index, value, hash) {
      if (node.word) {
        if (index >= word.length) {
          hash[node.word] = value;
        } else {
          hash[node.word] = false
        }
      }

      var a = word[index]
      for (let c in node.children) {
        if (c === a) { //都是大写或小写,并且字母一样
          innerSearch(node.children[c], word, index + 1, value, hash)
        } else {
          var code = c.charCodeAt(0)
          if (code >= 65 && code <= 90) {
            innerSearch(node.children[c], word, index, false, hash)
          } else {
            innerSearch(node.children[c], word, index, value, hash)
          }

        }
      }
      return true
    }

    function camelMatch(queries, pattern) {
      let trie = new Trie
      for (let word of queries) {
        trie.insert(word)
      }
      let hash = trie.search(pattern)
      let ret = []
      for (let word of queries) {
        ret.push(hash[word])
      }

      console.log(ret)
      return ret;
    }
    camelMatch(["FooBar", "FooBarTest", "FootBall", "FrameBuffer", "ForceFeedBack"], 'FB')
    camelMatch(["FooBar", "FooBarTest", "FootBall", "FrameBuffer", "ForceFeedBack"], 'FoBa')
    camelMatch(["FooBar", "FooBarTest", "FootBall", "FrameBuffer", "ForceFeedBack"], 'FBT')