当前位置:网站首页>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];
}
}
边栏推荐
- 多个kubernetes集群如何实现共享同一个存储
- 2022上半年朋友圈都在传的10本书,找到了
- Realize payment function in applet
- 解决rosdep的报错问题
- testing and SQA_动态白盒測试[通俗易懂]
- 编译原理 实验一:词法分析器的自动实现(Lex词法分析)
- 微服务远程Debug,Nocalhost + Rainbond微服务开发第二弹
- Chief technology officer of Pasqual: analog quantum computing takes the lead in bringing quantum advantages to industry
- L1-023 output gplt (Lua)
- How many times is PTA 1101 B than a
猜你喜欢

Numpy——axis

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

超分辨率技术在实时音视频领域的研究与实践

基于图像和激光的多模态点云融合与视觉定位

杰理之相同声道的耳机不允许配对【篇】

ES6笔记一

Creative changes brought about by the yuan universe

Basic concepts and properties of binary tree

Mathematical analysis_ Notes_ Chapter 11: Fourier series

链式二叉树的基本操作(C语言实现)
随机推荐
Responsibility chain model - unity
SD_ DATA_ SEND_ SHIFT_ REGISTER
Numpy——2. Shape of array
炒股如何开户?请问一下手机开户股票开户安全吗?
PTA 1102 teaching Super Champion volume
Redis
Teach your sister to write the message queue hand in hand
In 2021, the national average salary was released. Have you reached the standard?
Flipping Game(枚举)
[HDU] 5248 sequence transformation (greedy + dichotomy) [recommended collection]
testing and SQA_动态白盒測试[通俗易懂]
Mathematical analysis_ Notes_ Chapter 11: Fourier series
杰理之关于 TWS 交叉配对的配置【篇】
LeetCode 890(C#)
微服务远程Debug,Nocalhost + Rainbond微服务开发第二弹
Creative changes brought about by the yuan universe
Numpy——axis
Antisamy: a solution against XSS attack tutorial
Hongmeng smart home [1.0]
2022.07.02