当前位置:网站首页>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];
}
}
边栏推荐
- 链式二叉树的基本操作(C语言实现)
- L1-019 who falls first (Lua)
- Realize payment function in applet
- 【HDU】5248-序列变换(贪心+二分)「建议收藏」
- The top of slashdata developer tool is up to you!!!
- Is AI more fair than people in the distribution of wealth? Research on multiplayer game from deepmind
- SD_ DATA_ SEND_ SHIFT_ REGISTER
- 數據驗證框架 Apache BVal 再使用
- 初识缓存以及ehcache初体验「建议收藏」
- CMD command enters MySQL times service name or command error (fool teaching)
猜你喜欢
[tpm2.0 principle and Application guide] Chapter 9, 10 and 11
超分辨率技术在实时音视频领域的研究与实践
10 schemes to ensure interface data security
數據驗證框架 Apache BVal 再使用
Numpy——axis
杰理之测试盒配置声道【篇】
2022.07.04
Complete e-commerce system
I feel cheated. Wechat tests the function of "size number" internally, and two wechat can be registered with the same mobile number
杰理之手动配对方式【篇】
随机推荐
Business experience in virtual digital human
LeetCode 515(C#)
炒股如何开户?请问一下手机开户股票开户安全吗?
Version 2.0 of tapdata, the open source live data platform, has been released
Tapdata 的 2.0 版 ,开源的 Live Data Platform 现已发布
Flipping Game(枚举)
杰理之开机自动配对【篇】
How to implement safety practice in software development stage
Learn open62541 -- [67] add custom enum and display name
"Decryption" Huawei machine vision Corps: Huawei is moving up and the industry is moving forward
Command mode - unity
初识缓存以及ehcache初体验「建议收藏」
Where does brain hole come from? New research from the University of California: creative people's neural connections will "take shortcuts"
Embedded interview questions (algorithm part)
Chief technology officer of Pasqual: analog quantum computing takes the lead in bringing quantum advantages to industry
企业MES制造执行系统的分类与应用
ES6笔记一
PV静态创建和动态创建
微信网页调试8.0.19换掉X5内核,改用xweb,所以x5调试方式已经不能用了,现在有了解决方案
超分辨率技术在实时音视频领域的研究与实践