当前位置:网站首页>LeetCode 648(C#)
LeetCode 648(C#)
2022-07-07 19:20:00 【Just be interesting】
subject
In English , We have one called Root (root) The concept of , You can add other words after the root to form another longer word —— We call it Inheritance words (successor). for example , Root an, Follow the word other( other ), Can form new words another( the other one ).
Now? , Given a dictionary consisting of many roots dictionary And a sentence formed by separating words with spaces sentence. You need to replace all the inherited words in the sentence with roots . If an inherited word has many roots that can form it , Replace it with the shortest root .
You need to output the replaced sentences .
Example 1:
Input :dictionary = [“cat”,“bat”,“rat”], sentence = “the cattle was rattled by the battery”
Output :“the cat was rat by the bat”
Example 2:
Input :dictionary = [“a”,“b”,“c”], sentence = “aadsfasf absbs bbab cadsfafs”
Output :“a a b c”
Code
- Regular expressions
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;
}
}
- Hash enumeration
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);
}
}
- Prefix tree
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];
}
}
边栏推荐
- 50亿,福建又诞生一只母基金
- Number - number (Lua)
- First time in China! The language AI strength of this Chinese enterprise is recognized as No.2 in the world! Second only to Google
- How to estimate the value of "not selling pens" Chenguang?
- 3.关于cookie
- Mathematical analysis_ Notes_ Chapter 11: Fourier series
- Complete e-commerce system
- [Blue Bridge Cup training 100 questions] sort scratch from small to large. Blue Bridge Cup scratch competition special prediction programming question centralized training simulation exercise question
- Jerry's headphones with the same channel are not allowed to pair [article]
- [tpm2.0 principle and Application guide] Chapter 16, 17 and 18
猜你喜欢

2022上半年朋友圈都在传的10本书,找到了

cmd命令进入MySQL时报服务名或者命令错误(傻瓜式教学)
![[tpm2.0 principle and Application guide] Chapter 16, 17 and 18](/img/7a/b16549590e6445d9199c8000d47d36.png)
[tpm2.0 principle and Application guide] Chapter 16, 17 and 18

Numpy——axis

杰理之发起对耳配对、回连、开启可发现、可连接的轮循函数【篇】

Experiment 1 of Compilation Principle: automatic implementation of lexical analyzer (Lex lexical analysis)

Policy mode - unity

ES6笔记一

编译原理 实验一:词法分析器的自动实现(Lex词法分析)

Continuous test (CT) practical experience sharing
随机推荐
最长公共前缀(leetcode题14)
2022.07.05
咋吃都不胖的朋友,Nature告诉你原因:是基因突变了
unity2d的Rigidbody2D的MovePosition函数移动时人物或屏幕抖动问题解决
數據驗證框架 Apache BVal 再使用
IP netns command (memo)
超分辨率技术在实时音视频领域的研究与实践
Redis
UVALive – 4621 Cav 贪心 + 分析「建议收藏」
Longest common prefix (leetcode question 14)
6. About JWT
Hutool - lightweight DB operation solution
Do you know all four common cache modes?
99% of people don't know that privatized deployment is also a permanently free instant messaging software!
多个kubernetes集群如何实现共享同一个存储
ES6笔记一
Learn open62541 -- [67] add custom enum and display name
2022.07.05
LeetCode 890(C#)
杰理之关于 TWS 交叉配对的配置【篇】