From 0485d401144b0e09dbaee721fa33250454257775 Mon Sep 17 00:00:00 2001 From: yuanguangxin <274841922@qq.com> Date: Mon, 17 Aug 2020 03:32:13 +0800 Subject: [PATCH] add question 648 --- README.md | 4 ++ README_EN.md | 10 ++-- src/字典树/q648_单词替换/Solution.java | 49 +++++++++++++++++++ 3 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 src/字典树/q648_单词替换/Solution.java diff --git a/README.md b/README.md index 8b834ef..9064b17 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,10 @@ - [q56_合并区间](/src/区间合并/q56_合并区间) +### 字典树(前缀树) + +- [q648_单词替换](/src/字典树/q648_单词替换) + ### 字符串操作 - [q6_Z字形变换](/src/字符串操作/q6_Z字形变换) diff --git a/README_EN.md b/README_EN.md index ce6d77d..b589888 100644 --- a/README_EN.md +++ b/README_EN.md @@ -18,7 +18,7 @@ - [Question 138 : Copy List with Random Pointer](/src/链表操作/q138_复制带随机指针的链表) - [Question 206 : Reverse Linked List](/src/链表操作/q206_反转链表) -### Double Pointer Traversal / Sliding Window +### Two Pointers Traversal / Sliding Window - [Question 3 : Longest Substring Without Repeating Characters](/src/双指针遍历/q3_无重复字符的最长子串) - [Question 11 : Container With Most Water](/src/双指针遍历/q11_盛最多水的容器) @@ -29,7 +29,7 @@ - [Question 121 : Best Time to Buy and Sell Stock](/src/双指针遍历/q121_买卖股票的最佳时机) - [Question 209 : Minimum Size Subarray Sum](/src/双指针遍历/q209_长度最小的子数组) -### Fast and Slow Pointer Traversal +### Fast and Slow Pointers Traversal - [Question 141 : Linked List Cycle](/src/快慢指针遍历/q141_环形链表) - [Question 202 : Happy Number](/src/快慢指针遍历/q202_快乐数) @@ -39,6 +39,10 @@ - [Question 56 : Merge Intervals](/src/区间合并/q56_合并区间) +### Trie + +- [Question 648 : Replace Words](/src/字典树/q648_单词替换) + ### String Manipulation - [Question 6 : ZigZag Conversion](/src/字符串操作/q6_Z字形变换) @@ -118,7 +122,7 @@ - [Question 144 : Binary Tree Preorder Traversal](/src/树的遍历/q144_二叉树的前序遍历) - [Question 145 : Binary Tree Postorder Traversal](/src/树的遍历/q145_二叉树的后序遍历) -### Binary Search Trees +### Binary Search Tree - [Question 98 : Validate Binary Search Tree](/src/二叉搜索树相关/q98_验证二叉搜索树) - [Question 450 : Delete Node in a BST](/src/二叉搜索树相关/q450_删除二叉搜索树中的节点) diff --git a/src/字典树/q648_单词替换/Solution.java b/src/字典树/q648_单词替换/Solution.java new file mode 100644 index 0000000..fb040fc --- /dev/null +++ b/src/字典树/q648_单词替换/Solution.java @@ -0,0 +1,49 @@ +package 字典树.q648_单词替换; + +import java.util.List; + +/** + * 构建字典树(前缀树)o(n) + */ +class Solution { + public String replaceWords(List roots, String sentence) { + TrieNode trie = new TrieNode(); + for (String root : roots) { + TrieNode cur = trie; + for (char letter : root.toCharArray()) { + if (cur.children[letter - 'a'] == null) { + cur.children[letter - 'a'] = new TrieNode(); + } + cur = cur.children[letter - 'a']; + } + cur.word = root; + } + + StringBuilder ans = new StringBuilder(); + + for (String word : sentence.split(" ")) { + if (ans.length() > 0) { + ans.append(" "); + } + + TrieNode cur = trie; + for (char letter : word.toCharArray()) { + if (cur.children[letter - 'a'] == null || cur.word != null) { + break; + } + cur = cur.children[letter - 'a']; + } + ans.append(cur.word != null ? cur.word : word); + } + return ans.toString(); + } +} + +class TrieNode { + TrieNode[] children; + String word; + + TrieNode() { + children = new TrieNode[26]; + } +}