leetcode replacement word

Sword finger Offer II 063. Substitute words

Difficulty: * * in English, there is a name called   The concept of root, which can be followed by other words to form another longer word - we call this word   Successor. For example, the root an follows the word   Other can form new words   Another. Now, given a dictionary composed of many roots and a sentence, you need to replace all 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 sentence.   Example 1: input: Dictionary = ["cat", "bat", "rat"], sentence = "the cat was rated 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 aa aa aa aa aa aa aa aa aaaaaa BBB Baba ABA" output: "a a a BBB Baba a" example 4: input: Dictionary = ["cat", "cat", "bat", "rat"], sentence = "the cat was rated 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“   Tip: 1 < = dictionary.length  <= 1000 1 <= dictionary[i].length <= 100 dictionary[i]   It consists of lowercase letters only. 1 <= sentence.length <= 10^6 sentence   Only lowercase letters and spaces. The total number of words in sentence is in the range [1, 1000]. The length of each word in sentence is in the range [1, 1000]. Words in sentence are separated by a space. sentence   No leading or trailing spaces.   Note: this question is related to the main station 648   Same question:   https://leetcode-cn.com/problems/replace-words/ **

In English, there is one called   The concept of root, which can be followed by other words to form another longer word - we call this word   Successor. For example, the root an follows the word   Other can form new words   Another.

Now, given a dictionary composed of many roots and a sentence, you need to replace all 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 sentence.

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"

Tips:

  • 1 <= dictionary.length <= 1000
  • 1 <= dictionary[i].length <= 100
  • dictionary[i]   It consists of lowercase letters only.
  • 1 <= sentence.length <= 10^6
  • sentence   Only lowercase letters and spaces.
  • The total number of words in sentence is in the range [1, 1000].
  • The length of each word in sentence is in the range [1, 1000].
  • Words in sentence are separated by a space.
  • sentence   No leading or trailing spaces.

Note: this question is related to the main station 648   Same question:

Solution

Language: ****

type Node struct {
    child [26]*Node 
    isWord bool
}

func New() *Node {
    return &Node {
        child: [26]*Node{},
        isWord: false,
    }
}

func (node *Node) Insert(word string) {
    var cur = node 
    for _,c := range word  {
        if cur.child[c-'a'] == nil {
            cur.child[c-'a'] = New()
        }
        cur =  cur.child[c-'a' ]
    }
    cur.isWord= true
}
func (node *Node ) GetPrefix(word string) string {
    var cur = node 
    var res strings.Builder
    for _,c := range word {
        var i = c-'a'
        if cur.child[i] == nil {
            return word
        }
        res.WriteRune(c)
        cur = cur.child[i]
        if cur.isWord {
            return res.String()
        }

    }
    return word
}





func replaceWords(dictionary []string, sentence string) string {
    var root = New()
    for _, w := range dictionary {
        root.Insert(w)
    }
    var result = strings.Fields(sentence)
    for i:=0;i<len(result) ; i++ {
        result[i] = root.GetPrefix(result[i])
    } 
    return strings.Join(result," ")



}
​

Posted on Mon, 29 Nov 2021 16:47:46 -0500 by pete_bisby