LeetCode 648. 单词替换
文章目录
- 一、题目
- 1、题目描述
- 2、基础框架
- 3、原题链接
- 二、解题报告
- 1、思路分析
- 2、时间复杂度
- 3、代码详解
- 三、本题小知识
- 四、加群须知
一、题目
1、题目描述
在英语中,我们有一个叫做 词根(root) 的概念,可以词根后面添加其他一些词组成另一个较长的单词——我们称这个词为 继承词(successor)。例如,词根
an
,跟随着单词other
(其他),可以形成新的单词another
(另一个)。
现在,给定一个由许多词根组成的词典dictionary
和一个用空格分隔单词形成的句子sentence
。你需要将句子中的所有继承词用词根替换掉。如果继承词有许多可以形成它的词根,则用最短的词根替换它。
你需要输出替换之后的句子。
样例输入:dictionary = ["cat","bat","rat"], sentence = "the cattle was rattled by the battery"
样例输出:"the cat was rat by the bat"
2、基础框架
- C++ 版本给出的基础框架代码如下:
class Solution {public:string replaceWords(vector<string>& dictionary, string sentence) {}
};
3、原题链接
LeetCode 648. 单词替换
剑指 Offer II 063. 替换单词
二、解题报告
1、思路分析
(1)(1)(1) 首先对字典建立字典树,这样我们就能通过字典树进行前缀查找了。
(2)(2)(2) 对于句子sentence
把所有的单词通过空格分开来,然后单独进行计算。
(3)(3)(3) 对于个分割后的字符串的每个前缀,去字典树里面查找,有没有一个最短的前缀在字典中,直接返回这个前缀。
(3.1)(3.1)(3.1) 如果有结束标记,说明当前的字符串是原字典中的串,并且最短,直接返回记录的串ans
;
(3.2)(3.2)(3.2) 表示当前这个前缀不在字典里,所以直接返回完整的字符串即可;
(3.3)(3.3)(3.3) 表示当前给定的这个字符串,它是字典中某个字符串的前缀,那直接返回它本身即可;
string getMinPrefix(string word) {TrieNode *now = root;string ans = "";for(int i = 0; i < word.size(); ++i) {if(now->end) {return ans; // (3.1) }ans.push_back(word[i]);int child = word[i] - 'a';now = now->next[child];if(now == nullptr) { // (3.2)return word;}}return ans; // (3.3)}
2、时间复杂度
被查找的字符串长度为 nnn,最坏时间复杂度 O(n)O(n)O(n) 。
3、代码详解
class TrieNode {public:bool end; // 1 表示从根结点到它是一个完整字典中的串// 0 表示它是某个字符串的前缀TrieNode *next[26]; // 指向所有的子结点TrieNode() {end = false;memset(next, 0, sizeof(next));}
};class Trie {TrieNode* root;
public:Trie() {root = new TrieNode();}void insert(string word) {TrieNode *now = root;for(int i = 0; i < word.size(); ++i) {int child = word[i] - 'a';if( nullptr == now->next[child] ) {now->next[child] = new TrieNode();}now = now->next[child];}now->end = true;}bool search(string word) {TrieNode *now = root;for(int i = 0; i < word.size(); ++i) {int child = word[i] - 'a';if( nullptr == now->next[child]) {return false;}now = now->next[child];}return now->end;}bool startsWith(string prefix) {TrieNode *now = root;for(int i = 0; i < prefix.size(); ++i) {int child = prefix[i] - 'a';if( nullptr == now->next[child]) {return false;}now = now->next[child];}return true;}string getMinPrefix(string word) {TrieNode *now = root;string ans = "";for(int i = 0; i < word.size(); ++i) {if(now->end) {return ans;}ans.push_back(word[i]);int child = word[i] - 'a';now = now->next[child];if(now == nullptr) {return word;}}return ans;}
};class Solution {public:string replaceWords(vector<string>& dictionary, string sentence) {int i;Trie tree = Trie();for(i = 0; i < dictionary.size(); ++i) {tree.insert(dictionary[i]);}string str;string ret = "";int flag = 0;for(i = 0; i <= sentence.size(); ++i) {if(i == sentence.size() || sentence[i] == ' ') {if(flag++) {ret += " ";}ret += tree.getMinPrefix(str);str = "";}else {str.push_back(sentence[i]);}}return ret; }
};
三、本题小知识
凡是涉及到前缀的问题,一律优先考虑字典树。
四、加群须知
相信看我文章的大多数都是「 大学生 」,能上大学的都是「 精英 」,那么我们自然要「 精益求精 」,如果你还是「 大一 」,那么太好了,你拥有大把时间,当然你可以选择「 刷剧 」,然而,「 学好算法 」,三年后的你自然「 不能同日而语 」。
那么这里,我整理了「 几十个基础算法 」 的分类,点击开启:
Leetcode 648.单词替换 1 题目描述(Leetcode题目链接) 在英语中,我们有一个叫做 词根(root)的概念,它可以跟着其他一些词组成另一个较长的单词--我们称这个词为 继承词( ... 单词替换 在英语中,我们有一个叫做 词根(root)的概念,它可以跟着其他一些词组成另一个较长的单词--我们称这个词为 继承词(successor).例如,词根an,跟随着单词 other(其他),可 ... 1. 题目 在英语中,我们有一个叫做 词根(root)的概念,它可以跟着其他一些词组成另一个较长的单词--我们称这个词为 继承词(successor).例如,词根an,跟随着单词 other(其他), ... 这道题目的关键是,如果找到了前缀,就整个单词就只留下前缀,剩下的部分扔掉,如果没有找到前缀,整个单词都留下. string replaceWords(vector<string>& ... leetcode每日一题 ps:今天的每日一题没意思,简单的模拟,自己换一道 面试题 08.04. 幂集 幂集.编写一种方法,返回某集合的所有子集.集合中不包含重复的元素. 说明:解集不能包含重复的子 ... MyTrie结构体和相关操作函数 typedef struct MyTrie {bool is_word;vector<MyTrie*> next;MyTrie():is_word(fal ... 题目描述 这是 LeetCode 上的 648. 单词替换 ,难度为 中等. Tag : 「字典树」 在英语中,我们有一个叫做 词根(root) 的概念,可以词根后面添加其他一些词组成另一个较长的单词 ... 题目 在英语中,我们有一个叫做 词根(root) 的概念,可以词根后面添加其他一些词组成另一个较长的单词--我们称这个词为 继承词(successor).例如,词根an,跟随着单词 other(其他) ... 题目 示例 思路 前缀:字符串的前缀是指字符串的任意首部.比如字符串"abbc"的前缀有"a","ab","abb",& ...LeetCode 648. 单词替换相关推荐
最新文章
热门文章