LC 966. Vowel Spellchecker
Given a wordlist, we want to implement a spellchecker that converts a query word into a correct word.
For a given query word, the spell checker handles two categories of spelling mistakes:
- Capitalization: If the query matches a word in the wordlist (case-insensitive), then the query word is returned with the same case as the case in the wordlist.
- Example:
wordlist = ["yellow"],query = "YellOw":correct = "yellow" - Example:
wordlist = ["Yellow"],query = "yellow":correct = "Yellow" - Example:
wordlist = ["yellow"],query = "yellow":correct = "yellow"
- Example:
- Vowel Errors: If after replacing the vowels ('a', 'e', 'i', 'o', 'u') of the query word with any vowel individually, it matches a word in the wordlist (case-insensitive), then the query word is returned with the same case as the match in the wordlist.
- Example:
wordlist = ["YellOw"],query = "yollow":correct = "YellOw" - Example:
wordlist = ["YellOw"],query = "yeellow":correct = ""(no match) - Example:
wordlist = ["YellOw"],query = "yllw":correct = ""(no match)
- Example:
In addition, the spell checker operates under the following precedence rules:
- When the query exactly matches a word in the wordlist (case-sensitive), you should return the same word back.
- When the query matches a word up to capitlization, you should return the first such match in the wordlist.
- When the query matches a word up to vowel errors, you should return the first such match in the wordlist.
- If the query has no matches in the wordlist, you should return the empty string.
Given some queries, return a list of words answer, where answer[i] is the correct word for query = queries[i].
Example 1:
Input: wordlist = ["KiTe","kite","hare","Hare"], queries = ["kite","Kite","KiTe","Hare","HARE","Hear","hear","keti","keet","keto"]
Output: ["kite","KiTe","KiTe","Hare","hare","","","KiTe","","KiTe"]
Note:
1 <= wordlist.length <= 50001 <= queries.length <= 50001 <= wordlist[i].length <= 71 <= queries[i].length <= 7- All strings in
wordlistandqueriesconsist only of english letters.
踩坑踩了一个下午,掉到坑里出不来了。
Runtime: 96 ms, faster than 100.00% of C++ online submissions for Vowel Spellchecker.
第一种是我的做法 修改了TrieNode的结构,让它带了一个case_sensitive的标志位。如果带上大小写搜索,
每次需要判断字符的大小写,这样得到的是第一种准确的搜索。如果不敏感,就全部放在小写的这个节点里。
为了满足题意,大小写不敏感的可能有多种,那么Trie节点带一个数组比较合适。
注意:
1. 本来的字典可能有重复,我去。。。
2. 需要返回最小的index,那就把所有的求出来以后再求最小的index。
写了一个下午。
class TrieNode{
public:
string word;
vector<string> wordlist;
TrieNode* lowercase[];
TrieNode* uppercase[];
TrieNode(){
for(int i=; i<; i++) lowercase[i] = nullptr;
for(int i=; i<; i++) uppercase[i] = nullptr;
}
};
class Trie{
public:
TrieNode* root;
Trie(const vector<string>& wordlist, bool case_sensitive = true){
root = new TrieNode();
BuildTrie(wordlist, case_sensitive);
}
void BuildTrie(const vector<string>& wordlist, bool case_sensitive = true){
for(int i=; i<wordlist.size(); i++){
TrieNode* tmp = root;
for(int j=; j<wordlist[i].size(); j++){
char tmpchar = wordlist[i][j];
int idx = ;
if(!case_sensitive){
if(tmpchar >= 'A' && tmpchar <= 'Z'){
idx = tmpchar - 'A';
}else idx = tmpchar - 'a';
if(!tmp->lowercase[idx]) tmp->lowercase[idx] = new TrieNode();
tmp = tmp->lowercase[idx];
}else{
if(tmpchar >= 'A' && tmpchar <= 'Z'){
idx = tmpchar - 'A';
if(!tmp->uppercase[idx]) tmp->uppercase[idx] = new TrieNode();
tmp = tmp->uppercase[idx];
}else {
idx = tmpchar - 'a';
if(!tmp->lowercase[idx]) tmp->lowercase[idx] = new TrieNode();
tmp = tmp->lowercase[idx];
}
}
}
if(case_sensitive){
tmp->word = wordlist[i];
}else {
tmp->wordlist.push_back(wordlist[i]);
}
}
}
bool hasword(string word, string& foundword, bool case_sensitive = true){
TrieNode* tmp = root;
locale loc;
for(int i=; i<word.size(); i++){
char tmpchar = word[i];
int idx = ;
if(!case_sensitive){
tmpchar = tolower(tmpchar, loc);
idx = tmpchar - 'a';
if(!tmp->lowercase[idx]) return false;
tmp = tmp->lowercase[idx];
}else {
if(tmpchar >= 'A' && tmpchar <= 'Z') {
idx = tmpchar - 'A';
if(!tmp->uppercase[idx]) return false;
tmp = tmp->uppercase[idx];
} else {
idx = tmpchar - 'a';
if(!tmp->lowercase[idx]) return false;
tmp = tmp->lowercase[idx];
}
}
}
if(!case_sensitive) {
if(!tmp->wordlist.empty()){
foundword = tmp->wordlist[];
return true;
}
return false;
}
if(!tmp->word.empty()) {
foundword = tmp->word;
return true;
}
return false;
}
};
class Solution {
public:
set<char> vset;
unordered_map<string,int> mp;
vector<string> spellchecker(vector<string>& wordlist, vector<string>& queries) {
for(int i=; i<wordlist.size(); i++){
mp[wordlist[i]] = i;
}
vset.insert('a');vset.insert('e');vset.insert('i');
vset.insert('o');vset.insert('u');
vector<string> ret;
Trie trie_case_sensi = Trie(wordlist);
Trie trie_not_case_sensi = Trie(wordlist, false);
for(int i=; i<queries.size(); i++){
string foundword;
vector<string> foundwordvec;
if(trie_case_sensi.hasword(queries[i],foundword)) {
ret.push_back(queries[i]);
}else if(trie_not_case_sensi.hasword(queries[i], foundword, false)){
ret.push_back(foundword);
}else {
dfs(trie_not_case_sensi.root, queries[i], foundwordvec, );
int minidx = wordlist.size();
for(auto x : foundwordvec) minidx = min(minidx, mp[x]);
if(minidx == wordlist.size()) ret.push_back("");
else ret.push_back(wordlist[minidx]);
}
}
return ret;
}
void dfs(const TrieNode* root,string query, vector<string>& ret, int start){
if(start == query.size()){
assert(!root->wordlist.empty());
ret.push_back(root->wordlist[]);
return ;
}
std::locale loc;
char c = tolower(query[start],loc);
int idx = ;
idx = c - 'a';
if(vset.count(c)){
for(auto it = vset.begin(); it != vset.end(); it++){
char vsub = *it;
int newidx = ;
newidx = vsub - 'a';
if(!root->lowercase[newidx]) continue;
TrieNode* nextroot = root->lowercase[newidx];
dfs(nextroot, query, ret, start+);
}
}else{
if(!root->lowercase[idx]) return ;
TrieNode* nextroot = root->lowercase[idx];
dfs(nextroot, query, ret, start+);
}
}
};
下面看看网上的大神的解法,很简单,既然aeiou都能互相匹配,那就把aeiou变成#,这样一来不就行了么!真是厉害。
class Solution {
public String[] spellchecker(String[] wordlist, String[] queries) {
Set<String> words = new HashSet<>(Arrays.asList(wordlist));
HashMap<String, String> cap = new HashMap<>();
HashMap<String, String> vowel = new HashMap<>();
for (String w : wordlist) {
String lower = w.toLowerCase(), devowel = lower.replaceAll("[aeiou]", "#");
cap.putIfAbsent(lower, w);
vowel.putIfAbsent(devowel, w);
}
for (int i = ; i < queries.length; ++i) {
if (words.contains(queries[i])) continue;
String lower = queries[i].toLowerCase(), devowel = lower.replaceAll("[aeiou]", "#");
if (cap.containsKey(lower)) {
queries[i] = cap.get(lower);
} else if (vowel.containsKey(devowel)) {
queries[i] = vowel.get(devowel);
} else {
queries[i] = "";
}
}
return queries;
}
}
LC 966. Vowel Spellchecker的更多相关文章
- 【leetcode】966. Vowel Spellchecker
题目如下: Given a wordlist, we want to implement a spellchecker that converts a query word into a correc ...
- 【LeetCode】966. Vowel Spellchecker 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...
- [Swift]LeetCode966.元音拼写检查器 | Vowel Spellchecker
Given a wordlist, we want to implement a spellchecker that converts a query word into a correct word ...
- Weekly Contest 117
965. Univalued Binary Tree A binary tree is univalued if every node in the tree has the same value. ...
- 【Leetcode周赛】从contest-111开始。(一般是10个contest写一篇文章)
Contest 111 (题号941-944)(2019年1月19日,补充题解,主要是943题) 链接:https://leetcode.com/contest/weekly-contest-111 ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- 四种比较简单的图像显著性区域特征提取方法原理及实现-----> AC/HC/LC/FT。
laviewpbt 2014.8.4 编辑 Email:laviewpbt@sina.com QQ:33184777 最近闲来蛋痛,看了一些显著性检测的文章,只是简单的看看,并没有深入的研究,以 ...
- “LC.exe”错误
错误“LC.exe”已退出,代码为 -1. 可能的原因是: 这个第三方组件是个商业组件,他在组件的主使用类定义了 LicenseProvider(typeof(LicFileLicenseProvid ...
- 解决VS下“LC.exe已退出,代码为-1”问题
今天使用VS2015开发一个Winform程序,手一抖拖错了一个第三方控件,然后将其去掉并删除相关的引用,结果导致了LC.exe错误:"Lc.exe已退出,代码为-1 ". 经过上 ...
随机推荐
- /proc/filesystems各字段含义
/proc/filesystems A text listing of the filesystems which were compiled into the kernel. Incidentall ...
- BLE 5协议栈-通用属性规范层(GATT)
文章转载自:http://www.sunyouqun.com/2017/04/page/2/ 通用属性规范GATT(Generic Attribute Profile)将ATT层定义的属性打包成不同的 ...
- systemd自启动tomcat
tomcat自启动service [Unit] Description=Tomcat After=network.target [Service] Type=forking PIDFile=/usr/ ...
- 3.Shell 接收用户的参数
1.Shell 传递参数 我们可以在执行 Shell 脚本时,向脚本传递参数,Linux系统中的Shell脚本语言已经内设了用于接收参数的变量,变量之间可以使用空格间隔. 例如$0对应的是当前Shel ...
- Centos 安装 kubectl kubelet kubeadm
cat <<EOF > /etc/yum.repos.d/kubernetes.repo [kubernetes] name=Kubernetes baseurl=https://m ...
- 认识并初步应用GitHub
好好学习,天天向上 一.这是一个简单的开头 GIT的地址 https://github.com/Notexcellent GIT的用户名 Notexcxllent 学号后五位 82405 博客地址 h ...
- php的lareval框架配置出错
前两天,在学习php的 lareval 框架时,从官网上下载完lareval的安装包后,按照网上的配置教程一点一点的配置完后,当点击public 文件运行时就出现一个让我很头痛的问题,我自己外加两个大 ...
- 201871010101-陈来弟《面向对象程序设计(java)》第十七周学习总结
实验十七 线程同步控制 实验时间 2018-12-10 第一部分:理论知识 1.多线程并发执行中的问题 ◆多个线程相对执行的顺序是不确定的. ◆线程执行顺序的不确定性会产生执行结果的不确定性. ◆在 ...
- 【构造 meet in middle 随机 矩阵树定理】#75. 【UR #6】智商锁
没智商了 变式可见:[构造 思维题]7.12道路建设 当你自信满满地把你认为的正确密码输入后,时光机滴滴报警 —— 密码错误.你摊坐在了地上. 黑衣人满意地拍了拍你的肩膀:“小伙子,不错嘛.虽然没解开 ...
- Js基础知识(五) - 前端性能优化总结
前端性能优化总结 资源优化 缓存 最好的资源优化就是不加载资源.缓存也是最见效的优化手段.说实话,虽然说客户端缓存发生在浏览器端,但缓存主要还是服务端来控制,与我们前端关系并不是很大.但还是有必要了解 ...