当前位置:网站首页>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];
}
}
边栏推荐
- Pychart ide Download
- Skimage learning (3) -- adapt the gray filter to RGB images, separate colors by immunohistochemical staining, and filter the maximum value of the region
- LeetCode 1186. 删除一次得到子数组最大和 每日一题
- 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?
- [image sensor] correlated double sampling CDs
- Test case management tool recommendation
- dapp丨defi丨nft丨lp单双币流动性挖矿系统开发详细说明及源码
- Shallow understanding Net core routing
- LeetCode 403. Frog crossing the river daily
- SIGGRAPH 2022最佳技术论文奖重磅出炉!北大陈宝权团队获荣誉提名
猜你喜欢

Pisa-Proxy SQL 解析之 Lex & Yacc

最新阿里P7技术体系,妈妈再也不用担心我找工作了

How to add aplayer music player in blog

A tour of gRPC:03 - proto序列化/反序列化

Skimage learning (2) -- RGB to grayscale, RGB to HSV, histogram matching

Mrs offline data analysis: process OBS data through Flink job
![[image sensor] correlated double sampling CDs](/img/1c/3a641ad47ff91536db602dedc82705.png)
[image sensor] correlated double sampling CDs

MRS离线数据分析:通过Flink作业处理OBS数据

Skimage learning (3) -- gamma and log contrast adjustment, histogram equalization, coloring gray images

Master this promotion path and share interview materials
随机推荐
【饭谈】那些看似为公司着想,实际却很自私的故事 (一:造轮子)
Test case management tool recommendation
LeetCode 213. Home raiding II daily question
LeetCode 1155. N ways to roll dice one question per day
LeetCode 1049. Weight of the last stone II daily question
麒麟信安携异构融合云金融信创解决方案亮相第十五届湖南地区金融科技交流会
Module VI
Sator a lancé le jeu web 3 "satorspace" et a lancé huobi
[Seaborn] combination chart: pairplot and jointplot
国内首创!Todesk将RTC技术融入远程桌面,画质更清晰操作更流畅
centos7安装mysql笔记
mysql使用笔记一
服务器彻底坏了,无法修复,如何利用备份无损恢复成虚拟机?
【视频/音频数据处理】上海道宁为您带来Elecard下载、试用、教程
蓝桥杯 决赛 异或变换 100分
最新阿里P7技术体系,妈妈再也不用担心我找工作了
QT中自定义控件的创建到封装到工具栏过程(二):自定义控件封装到工具栏
Localstorage and sessionstorage
Flask搭建api服务-生成API文档
Flask搭建api服务-SQL配置文件