Leetcode-966 Vowel Spellchecker(元音拼写检查器)

 1 #define _for(i,a,b) for(int i = (a);i < (b);i ++)
 2 class Solution
 3 {
 4     public:
 5         vector<string> spellchecker(vector<string>& wordlist, vector<string>& queries)
 6         {
 7             vector<string> rnt;
 8             set<string> ws;
 9             map<string,int> wxs;
10             map<string,int> wys;
11
12             _for(i,0,wordlist.size())
13                 ws.insert(wordlist[i]);
14             _for(i,0,wordlist.size())
15             {
16                 string tmp = wordlist[i];
17                 _for(j,0,tmp.size())
18                     tmp[j] = tolower(tmp[j]);
19                 if(!wxs.count(tmp))
20                     wxs.insert({tmp,i});
21             }
22             _for(i,0,wordlist.size())
23             {
24                 string tmp = wordlist[i];
25                 _for(j,0,tmp.size())
26                 {
27                     tmp[j] = tolower(tmp[j]);
28                     if(tmp[j]==‘a‘||tmp[j]==‘e‘||tmp[j]==‘i‘
29                     ||tmp[j]==‘o‘||tmp[j]==‘u‘)
30                         tmp[j] = ‘*‘;
31                 }
32                 if(!wys.count(tmp))
33                     wys.insert({tmp,i});
34             }
35
36             _for(i,0,queries.size())
37             {
38                 if(ws.count(queries[i]))
39                     {rnt.push_back(queries[i]);continue;}
40
41                 string tmp = queries[i];
42                 _for(j,0,tmp.size())
43                     tmp[j] = tolower(tmp[j]);
44                 auto pp = wxs.find(tmp);
45                 if(pp!=wxs.end())
46                 {
47                     rnt.push_back(wordlist[pp->second]);
48                     continue;
49                 }
50
51                 _for(j,0,tmp.size())
52                     if(tmp[j]==‘a‘||tmp[j]==‘e‘||tmp[j]==‘i‘
53                     ||tmp[j]==‘o‘||tmp[j]==‘u‘)
54                         tmp[j]=‘*‘;
55                 auto pp2 = wys.find(tmp);
56                 if(pp2!=wys.end())
57                 {
58                     rnt.push_back(wordlist[pp2->second]);
59                     continue;
60                 }
61                 rnt.push_back("");
62             }
63             return rnt;
64         }
65 };

原文地址:https://www.cnblogs.com/Asurudo/p/10199702.html

时间: 2024-07-31 07:28:11

Leetcode-966 Vowel Spellchecker(元音拼写检查器)的相关文章

[Swift Weekly Contest 117]LeetCode966.元音拼写检查 | 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 (

一个简单的拼写检查器

记得以前就看过这篇文章:How to write a spelling corrector,文章将贝叶斯原理运用于拼写检查,二十几行简单的Python的代码就实现了一个拼写检查器. 感叹数学的美妙之外,也很喜欢类似这样的文章,将一个问题的原理讲清楚,再配上一些代码实例做说明,从小见大,从浅入深,对人很有启发. 这里简单的介绍下基于贝叶斯原理的拼写检查原理,再给出一个java版和C#版的实现. 拼写检查器的原理:给定一个单词,选择和它最相似的拼写正确的单词,需要使用概率论,而不是基于规则的判断.给

贝叶斯拼写检查器

本拼写检查器是基于朴素贝叶斯的基础来写的,贝叶斯公式以及原理就不在详述.直接上代码 import re, collections def words(text): return re.findall('[a-z]+', text.lower()) def train(features): model = collections.defaultdict(lambda : 1) for f in features: model[f] += 1 return model alphabet = 'abc

用贝叶斯实现拼写检查器

贝叶斯公式 p(A|D)=p(A)*p(D|A)/p(D); 可以应用于垃圾邮件的过滤和拼写检查 例如:对于拼写检查,写出一个单词D,判断该单词为正确单词A的概率.为上述条件概率的描述. 其中p(A)为先验概率,可以根据现有的数据库中的单词,来获得A单词的概率p(A).由于正确的单词不仅仅有A,还有可能有A1,A2.... 最终比较p(A1|D),p(A2|D),p(A3|D)...由于分母比较时相同,可以只比较分子p(A)*p(D|A) p(A|D)正比于p(A)*p(D|A) 分别计算p(A

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 (

【leetcode】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 word

[LeetCode] Strong Password Checker 密码强度检查器

A password is considered strong if below conditions are all met: It has at least 6 characters and at most 20 characters. It must contain at least one lowercase letter, at least one uppercase letter, and at least one digit. It must NOT contain three r

How to Write a Spelling Corrector用java 写拼写检查器 Java实现 以备查验

import java.io.*;import java.util.*;import java.util.regex.*; class Spelling { private final HashMap<String, Integer> nWords = new HashMap<String, Integer>(); public Spelling(String file) throws IOException { BufferedReader in = new BufferedRe

贝叶斯实现拼写检查器

import re,collections # 把语料中的单词全部抽取出来,转成小写,并且取出单词中间的特殊符号 def words(text): return re.findall('[a-z]+',text.lower()) def train(features): model = collections.defaultdict(lambda:1) # 词频的默认出现数为1 for f in features: model[f] += 1 return model NWORDS = trai