当前位置:网站首页>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];
}
}
边栏推荐
- Skimage learning (3) -- gamma and log contrast adjustment, histogram equalization, coloring gray images
- Smart logistics platform: make overseas warehouses smarter
- LeetCode 1654. 到家的最少跳跃次数 每日一题
- MySQL implements the query of merging two fields into one field
- skimage学习(1)
- Sator launched Web3 game "satorspace" and launched hoobi
- LeetCode 1696. Jumping game VI daily question
- 管理VDI的几个最佳实践
- LeetCode 300. Daily question of the longest increasing subsequence
- LeetCode 1155. 掷骰子的N种方法 每日一题
猜你喜欢

Mrs offline data analysis: process OBS data through Flink job

Skimage learning (3) -- adapt the gray filter to RGB images, separate colors by immunohistochemical staining, and filter the maximum value of the region
Direct dry goods, 100% praise

Pycharm IDE下载

麒麟信安操作系统衍生产品解决方案 | 存储多路径管理系统,有效提高数据传输可靠性

浅浅理解.net core的路由

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

Sator推出Web3游戏“Satorspace” ,并上线Huobi

The process of creating custom controls in QT to encapsulating them into toolbars (II): encapsulating custom controls into toolbars

Lowcode: four ways to help transportation companies enhance supply chain management
随机推荐
[image sensor] correlated double sampling CDs
测试用例管理工具推荐
Nerf: the ultimate replacement for deepfake?
【Seaborn】组合图表、多子图的实现
如何在软件研发阶段落地安全实践
DNS series (I): why does the updated DNS record not take effect?
LeetCode 1043. 分隔数组以得到最大和 每日一题
麒麟信安携异构融合云金融信创解决方案亮相第十五届湖南地区金融科技交流会
智慧物流平台:让海外仓更聪明
[video / audio data processing] Shanghai daoning brings you elecard download, trial and tutorial
LeetCode 1043. Separate the array to get the maximum and daily questions
LeetCode 1774. The dessert cost closest to the target price is one question per day
赋能智慧电力建设 | 麒麟信安高可用集群管理系统,保障用户关键业务连续性
skimage学习(3)——使灰度滤镜适应 RGB 图像、免疫组化染色分离颜色、过滤区域最大值
Lowcode: four ways to help transportation companies enhance supply chain management
麒麟信安云平台全新升级!
The mail server is listed in the blacklist. How to unblock it quickly?
Seaborn数据可视化
LeetCode 213. Home raiding II daily question
国内首创!Todesk将RTC技术融入远程桌面,画质更清晰操作更流畅