当前位置:网站首页>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];
}
}
边栏推荐
- Basic concepts and properties of binary tree
- Draw squares with Obama (Lua)
- 企业MES制造执行系统的分类与应用
- Complete e-commerce system
- SD_ DATA_ SEND_ SHIFT_ REGISTER
- Classification and application of enterprise MES Manufacturing Execution System
- Hongmeng smart home [1.0]
- SD_ DATA_ RECEIVE_ SHIFT_ REGISTER
- 嵌入式面试题(算法部分)
- 解决rosdep的报错问题
猜你喜欢
Redis
App capture of charles+postern
杰理之测试盒配置声道【篇】
Responsibility chain model - unity
[information security laws and regulations] review
[tpm2.0 principle and Application guide] Chapter 16, 17 and 18
多个kubernetes集群如何实现共享同一个存储
[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
How to estimate the value of "not selling pens" Chenguang?
RISCV64
随机推荐
L1-023 output gplt (Lua)
Big Ben (Lua)
2022.07.05
Antisamy: a solution against XSS attack tutorial
Former richest man, addicted to farming
How to implement safety practice in software development stage
二叉树的基本概念和性质
PTA 1101 B是A的多少倍
First time in China! The language AI strength of this Chinese enterprise is recognized as No.2 in the world! Second only to Google
2022.07.02
How to choose the appropriate automated testing tools?
ip netns 命令(备忘)
Learn open62541 -- [67] add custom enum and display name
【MIME笔记】
Chief technology officer of Pasqual: analog quantum computing takes the lead in bringing quantum advantages to industry
Basic concepts and properties of binary tree
數據驗證框架 Apache BVal 再使用
cmd命令进入MySQL时报服务名或者命令错误(傻瓜式教学)
炒股如何开户?请问一下手机开户股票开户安全吗?
In 2021, the national average salary was released. Have you reached the standard?