# 648 Replace Words

Description

In English, we have a concept called root, which can be followed by some other word to form another longer word - let’s call this word successor. For example, when the root “an” is followed by the successor word “other”, we can form a new word “another”.

Given a dictionary consisting of many roots and a sentence consisting of words separated by spaces, replace all the successors in the sentence with the root forming it. If a successor can be replaced by more than one root, replace it with the root that has the shortest length.

Return the sentence after the replacement.

Examples

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”

Example 3:

Input: dictionary = [“a”, “aa”, “aaa”, “aaaa”], sentence = “a aa a aaaa aaa aaa aaa aaaaaa bbb baba ababa”
Output: “a a a a a a a a bbb baba a”

Example 4:

Input: dictionary = [“catt”,“cat”,“bat”,“rat”], sentence = “the cattle was rattled by the battery”
Output: “the cat was rat by the bat”

Example 5:

Input: dictionary = [“ac”,“ab”], sentence = “it is abnormal that this solution is accepted”
Output: “it is ab that this solution is ac”

Constraints:

1 <= dictionary.length <= 1000
1 <= dictionary[i].length <= 100
dictionary[i] consists of only lower-case letters.
1 <= sentence.length <= 10^6
sentence consists of only lower-case letters and spaces.
The number of words in sentence is in the range [1, 1000]
The length of each word in sentence is in the range [1, 1000]
Each two consecutive words in sentence will be separated by exactly one space.
sentence does not have leading or trailing spaces.

思路

因为是按照分类刷的题,所以提前就知道要用前缀树的方法做
题目给出了一个前缀字典和一个字符串,对字符串中的每个单词进行判断,如果符合字典中的前缀,就用前缀进行替代,否则依旧为他本身
需要注意的是,如果字典中的两个前缀,其中一个前缀为另一个前缀的前缀(如a, aa,a为aa的前缀),则只会识别a而不会识别aa

因此完整的流程分为两步

  • 第一步是根据字典构建前缀树
  • 第二步是对字符串中的每个单词,与前缀树进行判断,这里每执行一个单词都要判断一下是否到了end(也就是字典中是否存在该单词),并进行替换/保留操作

以example4为例
首先是构建字典的前缀树,构建的流程就不赘述了,本次和208的构建有点区别,是用了TreeNode [26]数组和isEnd标识作为一个TreeNode,具体可以看代码,这里直接给出前缀树的结构
在这里插入图片描述
然后对字符串进行匹配[the cattle was rattled by the battery]
不难发现虽然cattle能匹配上catt,但是由于每个字母都会判定是否为end,因此到cat的时候就直接输出cat了

代码

class TreeNode {
    boolean isEnd;
    TreeNode[] children = new TreeNode[26];
    public TreeNode(){
        for(TreeNode child: children){
            child = null;
        }
        isEnd = false;
    }
    
    public void setEnd(){
        isEnd = true;
    }
    
    public boolean contains(int num){
        return children[num] != null;
    }
    
    public void addChild(int num){
        children[num] = new TreeNode();
    }
}

class Solution {
    public String replaceWords(List<String> dictionary, String sentence) {
        TreeNode root = new TreeNode();
        for(String dic: dictionary){
            TreeNode node = root;
            for(int i = 0; i < dic.length(); i++){
                int currentNum = dic.charAt(i) - 'a';
                if(node.contains(currentNum)){
                    if (node.isEnd)
                        break;
                }
                else{
                    node.addChild(currentNum);
                }
                node = node.children[currentNum];
            }
            node.setEnd();
        }
        
        String[] sentences = sentence.split(" ");
        String answer = "";
        for(String s: sentences){
            String temp = "";
            TreeNode node = root;
            boolean flag = false;
            for(int i = 0; i < s.length(); i++){
                int currentNum = s.charAt(i) - 'a';
                temp = temp + s.charAt(i);
                if(node.contains(currentNum)){
                    if (node.children[currentNum].isEnd){
                        flag = true;
                        break;
                    }
                    node = node.children[currentNum];
                }
                else{
                    break;
                }
            }
            if(flag)
                answer = answer + temp + " ";
            else
                answer = answer + s + " ";
        }
        return answer.strip();    
    }
}
### 回答1: Python敏感词过滤replace是一种用Python编程语言实现的敏感词过滤方法,它可以通过替换敏感词来达到过滤的目的。在实现过程中,可以使用Python的字符串替换函数replace()来实现敏感词的替换操作。该方法可以应用于各种文本处理场景,如社交媒体、聊天应用、论坛等。 ### 回答2: Python中的敏感词过滤可以通过字符串的`replace`方法实现。 `replace`方法用于将指定的字符串替换成新的字符串,可以实现简单的敏感词过滤。以下是一个示例代码: ```python sensitive_words = ['敏感词1', '敏感词2', '敏感词3'] # 敏感词列表 def filter_sensitive_words(text): for word in sensitive_words: text = text.replace(word, '*' * len(word)) # 将敏感词替换成相同长度的* return text # 示例 text = '这句话包含敏感词1和敏感词2' filtered_text = filter_sensitive_words(text) print(filtered_text) # 输出:'这句话包含***和***' ``` 以上代码将字符串中的敏感词替换成相同长度的`*`字符,实现了敏感词过滤。 需要注意的是,这种简单的replace方法只能替换整个敏感词,无法处理各种变种形式的敏感词,例如大小写变化、字符替换等。对于更复杂的敏感词过滤需求,可以使用正则表达式等更高级的方法。 另外,敏感词过滤是一个敏感的话题,需要结合具体项目需求和合法性考虑。在实际应用中,建议使用专门的敏感词过滤库或服务来处理。 ### 回答3: Python中可以使用字符串的replace方法来实现敏感词过滤。下面是一个例子: ```python sensitive_words = ["敏感词1", "敏感词2", "敏感词3"] # 敏感词列表 text = "这是一句包含敏感词1的句子" # 需要过滤的文本 for word in sensitive_words: text = text.replace(word, "*"*len(word)) # 将敏感词替换成等长的"*" print(text) ``` 这个例子中,我们首先定义了一个包含敏感词的列表`sensitive_words`,然后定义了一个需要过滤的文本`text`。然后使用for循环遍历敏感词列表,使用字符串的replace方法将每个敏感词替换成等长的"*"。执行完循环后,`text`中的敏感词就都被替换成了"*"。最后打印输出`text`。 通过使用字符串的replace方法,我们可以对文本中的敏感词进行替换操作,达到敏感词过滤的效果。当然,这只是一个简单的示例,实际应用中我们还可以根据需求对替换规则进行更复杂的设计。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值