当前位置:网站首页>LeetCode 648(C#)
LeetCode 648(C#)
2022-07-07 15:38:00 【有趣就行】
题目
在英语中,我们有一个叫做 词根(root) 的概念,可以词根后面添加其他一些词组成另一个较长的单词——我们称这个词为 继承词(successor)。例如,词根an,跟随着单词 other(其他),可以形成新的单词 another(另一个)。
现在,给定一个由许多词根组成的词典 dictionary 和一个用空格分隔单词形成的句子 sentence。你需要将句子中的所有继承词用词根替换掉。如果继承词有许多可以形成它的词根,则用最短的词根替换它。
你需要输出替换之后的句子。
示例 1:
输入:dictionary = [“cat”,“bat”,“rat”], sentence = “the cattle was rattled by the battery”
输出:“the cat was rat by the bat”
示例 2:
输入:dictionary = [“a”,“b”,“c”], sentence = “aadsfasf absbs bbab cadsfafs”
输出:“a a b c”
代码
- 正则表达式
using System.Text.RegularExpressions;
public class Solution
{
public string ReplaceWords(IList<string> dictionary, string sentence)
{
foreach (var dic in dictionary)
{
string regex = @$"((?<= )({
dic})\w+|^({
dic})\w+)";
sentence = Regex.Replace(sentence, regex, dic);
}
return sentence;
}
}
- 哈希枚举
public class Solution
{
public string ReplaceWords(IList<string> dictionary, string sentence)
{
ISet<string> set = new HashSet<string>();
foreach (var dic in dictionary)
set.Add(dic);
string[] words = sentence.Split(" ");
for (int i = 0; i < words.Length; i++)
{
string word = words[i];
for (int j = 0; j < word.Length - 1; j++)
{
if (set.Contains(word[0..(j + 1)]))
{
words[i] = word[0..(j + 1)];
break;
}
}
}
return string.Join(" ", words);
}
}
- 前缀树
public class Solution
{
private Trie _root = new Trie();
public string ReplaceWords(IList<string> dictionary, string sentence)
{
foreach (var s in dictionary) Add(s);
StringBuilder sb = new StringBuilder();
foreach (var s in sentence.Split(" "))
sb.Append(Query(s)).Append(" ");
sb.Length = sb.Length - 1;
return sb.ToString();
}
private void Add(string s)
{
Trie p = _root;
for (int i = 0; i < s.Length; i++)
{
int u = s[i] - 'a';
if (p.childer[u] == null)
p.childer[u] = new Trie();
p = p.childer[u];
}
p.isEnd = true;
}
private string Query(string s)
{
Trie p = _root;
for (int i = 0; i < s.Length; i++)
{
int u = s[i] - 'a';
if (p.childer[u] == null) break;
if (p.childer[u].isEnd) return s[0..(i + 1)];
p = p.childer[u];
}
return s;
}
private class Trie
{
public bool isEnd;
public Trie[] childer = new Trie[26];
}
}
边栏推荐
- 一文读懂数仓中的pg_stat
- Flask搭建api服务-生成API文档
- Shallow understanding Net core routing
- LeetCode 300. Daily question of the longest increasing subsequence
- 【图像传感器】相关双采样CDS
- MySQL implements the query of merging two fields into one field
- Pycharm IDE下载
- LeetCode 1049. 最后一块石头的重量 II 每日一题
- QT中自定义控件的创建到封装到工具栏过程(二):自定义控件封装到工具栏
- Localstorage and sessionstorage
猜你喜欢
Test case management tool recommendation
QT中自定义控件的创建到封装到工具栏过程(二):自定义控件封装到工具栏
AI来搞财富分配比人更公平?来自DeepMind的多人博弈游戏研究
《产品经理必读:五种经典的创新思维模型》的读后感
Skimage learning (3) -- gamma and log contrast adjustment, histogram equalization, coloring gray images
最新高频Android面试题目分享,带你一起探究Android事件分发机制
[Seaborn] combination chart: facetgrid, jointgrid, pairgrid
Pychart ide Download
skimage学习(2)——RGB转灰度、RGB 转 HSV、直方图匹配
skimage学习(3)——使灰度滤镜适应 RGB 图像、免疫组化染色分离颜色、过滤区域最大值
随机推荐
【视频/音频数据处理】上海道宁为您带来Elecard下载、试用、教程
Flask搭建api服务
自定义View必备知识,Android研发岗必问30+道高级面试题
麒麟信安加入宁夏商用密码协会
Rpcms method of obtaining articles under the specified classification
The computer cannot add a domain, and the Ping domain name is displayed as the public IP. What is the problem? How to solve it?
LeetCode 1186. 删除一次得到子数组最大和 每日一题
skimage学习(3)——使灰度滤镜适应 RGB 图像、免疫组化染色分离颜色、过滤区域最大值
LeetCode刷题day49
LeetCode 213. Home raiding II daily question
Jenkins发布uniapp开发的H5遇到的问题
LeetCode 1031. Maximum sum of two non overlapping subarrays
Skimage learning (2) -- RGB to grayscale, RGB to HSV, histogram matching
[Seaborn] implementation of combined charts and multi subgraphs
Master this promotion path and share interview materials
Flask build API service SQL configuration file
最新阿里P7技术体系,妈妈再也不用担心我找工作了
[fan Tan] those stories that seem to be thinking of the company but are actually very selfish (I: building wheels)
SlashData开发者工具榜首等你而定!!!
Arduino 控制的双足机器人