当前位置:网站首页>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];
    }
}
原网站

版权声明
本文为[Just be interesting]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071515232549.html