当前位置:网站首页>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];
}
}
边栏推荐
- 最新阿里P7技术体系,妈妈再也不用担心我找工作了
- Leetcode brush questions day49
- LeetCode 403. 青蛙过河 每日一题
- Process from creation to encapsulation of custom controls in QT to toolbar (I): creation of custom controls
- Shallow understanding Net core routing
- How to implement safety practice in software development stage
- 如何在软件研发阶段落地安全实践
- LeetCode 1477. 找两个和为目标值且不重叠的子数组 每日一题
- 服务器彻底坏了,无法修复,如何利用备份无损恢复成虚拟机?
- 如何在博客中添加Aplayer音乐播放器
猜你喜欢

Skimage learning (1)

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

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

Siggraph 2022 best technical paper award comes out! Chen Baoquan team of Peking University was nominated for honorary nomination

Sator a lancé le jeu web 3 "satorspace" et a lancé huobi

【Seaborn】组合图表:FacetGrid、JointGrid、PairGrid

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

Matplotlib绘制三维图形

SlashData开发者工具榜首等你而定!!!

Sator launched Web3 game "satorspace" and launched hoobi
随机推荐
dapp丨defi丨nft丨lp单双币流动性挖矿系统开发详细说明及源码
Seaborn data visualization
浅浅理解.net core的路由
A tour of grpc:03 - proto serialization / deserialization
浅谈 Apache Doris FE 处理查询 SQL 源码解析
The top of slashdata developer tool is up to you!!!
[Seaborn] implementation of combined charts and multi subgraphs
The process of creating custom controls in QT to encapsulating them into toolbars (II): encapsulating custom controls into toolbars
How to mount the original data disk without damage after the reinstallation of proxmox ve?
Flash build API service
Nerf: the ultimate replacement for deepfake?
99% 用户在 Power BI 云端报表常犯错误
NeRF:DeepFake的最终替代者?
On Apache Doris Fe processing query SQL source code analysis
防火墙系统崩溃、文件丢失的修复方法,材料成本0元
AI来搞财富分配比人更公平?来自DeepMind的多人博弈游戏研究
赋能智慧电力建设 | 麒麟信安高可用集群管理系统,保障用户关键业务连续性
Flask搭建api服务-生成API文档
LeetCode 1049. 最后一块石头的重量 II 每日一题
LeetCode 1031. Maximum sum of two non overlapping subarrays