cs: add «2707. Extra Characters in a String»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
1ac61b1a21
commit
5d40a9cbd4
1 changed files with 50 additions and 0 deletions
50
cs/extra-characters-in-a-string.cs
Normal file
50
cs/extra-characters-in-a-string.cs
Normal file
|
@ -0,0 +1,50 @@
|
|||
public class Solution {
|
||||
private class Node {
|
||||
public Dictionary<char, Node> Next { get; set; } = new();
|
||||
public bool Has { get; set; } = false;
|
||||
|
||||
public Node() { }
|
||||
}
|
||||
|
||||
private static Node MakeTrie(string[] dictionary) {
|
||||
var root = new Node();
|
||||
|
||||
foreach (var word in dictionary) {
|
||||
var node = root;
|
||||
foreach (var c in word) {
|
||||
if (!node.Next.ContainsKey(c)) {
|
||||
node.Next[c] = new();
|
||||
}
|
||||
node = node.Next[c];
|
||||
}
|
||||
node.Has = true;
|
||||
}
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
public int MinExtraChar(string s, string[] dictionary) {
|
||||
var trie = MakeTrie(dictionary);
|
||||
|
||||
var length = s.Length;
|
||||
var dp = new int[length + 1];
|
||||
|
||||
for (var start = length - 1; start >= 0; --start) {
|
||||
dp[start] = 1 + dp[start + 1];
|
||||
|
||||
var node = trie;
|
||||
for (var end = start; end < length; ++end) {
|
||||
if (!node.Next.ContainsKey(s[end])) {
|
||||
break;
|
||||
}
|
||||
|
||||
node = node.Next[s[end]];
|
||||
if (node.Has) {
|
||||
dp[start] = Math.Min(dp[start], dp[end + 1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return dp[0];
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue