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

推荐订阅源

SecWiki News
SecWiki News
Microsoft Azure Blog
Microsoft Azure Blog
V2EX - 技术
V2EX - 技术
N
News and Events Feed by Topic
Webroot Blog
Webroot Blog
博客园_首页
月光博客
月光博客
N
News | PayPal Newsroom
The Cloudflare Blog
博客园 - 聂微东
酷 壳 – CoolShell
酷 壳 – CoolShell
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
量子位
G
Google Developers Blog
T
Troy Hunt's Blog
博客园 - Franky
腾讯CDC
S
Security Affairs
J
Java Code Geeks
aimingoo的专栏
aimingoo的专栏
S
Security @ Cisco Blogs
www.infosecurity-magazine.com
www.infosecurity-magazine.com
The Last Watchdog
The Last Watchdog
B
Blog RSS Feed
D
DataBreaches.Net
Recorded Future
Recorded Future
H
Heimdal Security Blog
V
Vulnerabilities – Threatpost
Apple Machine Learning Research
Apple Machine Learning Research
云风的 BLOG
云风的 BLOG
博客园 - 司徒正美
D
Docker
P
Proofpoint News Feed
V
V2EX
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
S
Secure Thoughts
Engineering at Meta
Engineering at Meta
PCI Perspectives
PCI Perspectives
宝玉的分享
宝玉的分享
The Hacker News
The Hacker News
有赞技术团队
有赞技术团队
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Cloudbric
Cloudbric
Microsoft Security Blog
Microsoft Security Blog
G
GRAHAM CLULEY
MyScale Blog
MyScale Blog
L
LINUX DO - 热门话题
雷峰网
雷峰网
Know Your Adversary
Know Your Adversary

博客园 - 司徒正美

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')