zoukankan      html  css  js  c++  java
  • 648. Replace Words

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

    Now, given a dictionary consisting of many roots and a sentence. You need to replace all the successor in the sentence with the rootforming it. If a successor has many roots can form it, replace it with the root with the shortest length.

    You need to output the sentence after the replacement.

    Example 1:

    Input: dict = ["cat", "bat", "rat"]
    sentence = "the cattle was rattled by the battery"
    Output: "the cat was rat by the bat"

    Note:

    1. The input will only have lower-case letters.
    2. 1 <= dict words number <= 1000
    3. 1 <= sentence words number <= 1000
    4. 1 <= root length <= 100
    5. 1 <= sentence words length <= 1000

    题目含义:英文中,以较短的单词为前缀,可以构成较长的单词。此时前缀可以称为“词根”。

    给定一组词根字典dict,一个句子sentence。将句中的单词换为字典中出现过的最短词根。

    思路:字典树(Trie)。利用词根dict构建字典树trie,遍历sentence中的word,在trie中进行搜索。

     1 class Solution {
     2     
     3     class TrieNode {
     4         public char val;
     5         public boolean isWord;
     6         public TrieNode[] children = new TrieNode[26];
     7         public TrieNode() {}
     8         TrieNode(char c){
     9             val = c;
    10         }
    11     }    
    12     
    13     public String replaceWords(List<String> dict, String sentence) {
    14         String[] tokens = sentence.split(" ");
    15         TrieNode trie = buildTrie(dict);
    16         return replaceWords(tokens, trie);
    17     }
    18 
    19     private String replaceWords(String[] tokens, TrieNode root) {
    20         StringBuilder stringBuilder = new StringBuilder();
    21         for (String token : tokens) {
    22             stringBuilder.append(getShortestReplacement(token, root));
    23             stringBuilder.append(" ");
    24         }
    25         return stringBuilder.substring(0, stringBuilder.length()-1);
    26     }
    27 
    28     private String getShortestReplacement(String token, final TrieNode root) {
    29         TrieNode temp = root;
    30         StringBuilder stringBuilder = new StringBuilder();
    31         for (char c : token.toCharArray()) {
    32             stringBuilder.append(c);
    33             if (temp.children[c - 'a'] != null) {
    34                 if (temp.children[c - 'a'].isWord) {
    35                     return stringBuilder.toString();
    36                 }
    37                 temp = temp.children[c - 'a'];
    38             } else {
    39                 return token;
    40             }
    41         }
    42         return token;
    43     }
    44 
    45     private TrieNode buildTrie(List<String> dict) {
    46         TrieNode root = new TrieNode(' ');
    47         for (String word : dict) {
    48             TrieNode temp = root;
    49             for (char c : word.toCharArray()) {
    50                 if (temp.children[c - 'a'] == null) {
    51                     temp.children[c - 'a'] = new TrieNode(c);
    52                 }
    53                 temp = temp.children[c - 'a'];
    54             }
    55             temp.isWord = true;
    56         }
    57         return root;
    58     }       
    59 }
  • 相关阅读:
    团队个人冲刺day08
    4.26 jQuery AJAX load() 方法
    4.23 jquery
    4.22 AJAX技术
    4.21 正则表达式
    4.20
    4.15 重写团队作业1
    4.12 重写团队作业1
    4.9 团队作业1
    4.7 团队作业1
  • 原文地址:https://www.cnblogs.com/wzj4858/p/7722342.html
Copyright © 2011-2022 走看看