1. 程式人生 > 其它 >leetcode 替換單詞

leetcode 替換單詞

劍指 Offer II 063. 替換單詞

Difficulty: **在英語中,有一個叫做詞根(root) 的概念,它可以跟著其他一些片語成另一個較長的單詞——我們稱這個詞為繼承詞(successor)。例如,詞根an,跟隨著單詞other(其他),可以形成新的單詞another(另一個)。 現在,給定一個由許多詞根組成的詞典和一個句子,需要將句子中的所有繼承詞用詞根替換掉。如果繼承詞有許多可以形成它的詞根,則用最短的詞根替換它。 需要輸出替換之後的句子。 示例 1: 輸入:dictionary = ["cat","bat","rat"], sentence = "the cattle was rattled by the battery" 輸出:"the cat was rat by the bat" 示例 2: 輸入:dictionary = ["a","b","c"], sentence = "aadsfasf absbs bbab cadsfafs" 輸出:"a a b c" 示例 3: 輸入:dictionary = ["a", "aa", "aaa", "aaaa"], sentence = "a aa a aaaa aaa aaa aaa aaaaaa bbb baba ababa" 輸出:"a a a a a a a a bbb baba a" 示例 4: 輸入:dictionary = ["catt","cat","bat","rat"], sentence = "the cattle was rattled by the battery" 輸出:"the cat was rat by the bat" 示例 5: 輸入:dictionary = ["ac","ab"], sentence = "it is abnormal that this solution is accepted" 輸出:"it is ab that this solution is ac" 提示: 1 <= dictionary.length<= 1000 1 <= dictionary[i].length <= 100 dictionary[i]僅由小寫字母組成。 1 <= sentence.length <= 10^6 sentence僅由小寫字母和空格組成。 sentence 中單詞的總量在範圍 [1, 1000] 內。 sentence 中每個單詞的長度在範圍 [1, 1000] 內。 sentence 中單詞之間由一個空格隔開。 sentence沒有前導或尾隨空格。 注意:本題與主站 648題相同:

https://leetcode-cn.com/problems/replace-words/ **

在英語中,有一個叫做詞根(root) 的概念,它可以跟著其他一些片語成另一個較長的單詞——我們稱這個詞為繼承詞(successor)。例如,詞根an,跟隨著單詞other(其他),可以形成新的單詞another(另一個)。

現在,給定一個由許多詞根組成的詞典和一個句子,需要將句子中的所有繼承詞詞根替換掉。如果繼承詞有許多可以形成它的詞根,則用最短的詞根替換它。

需要輸出替換之後的句子。

示例 1:

輸入:dictionary = ["cat","bat","rat"], sentence = "the cattle was rattled by the battery"
輸出:"the cat was rat by the bat"

示例 2:

輸入:dictionary = ["a","b","c"], sentence = "aadsfasf absbs bbab cadsfafs"
輸出:"a a b c"

示例 3:

輸入:dictionary = ["a", "aa", "aaa", "aaaa"], sentence = "a aa a aaaa aaa aaa aaa aaaaaa bbb baba ababa"
輸出:"a a a a a a a a bbb baba a"

示例 4:

輸入:dictionary = ["catt","cat","bat","rat"], sentence = "the cattle was rattled by the battery"
輸出:"the cat was rat by the bat"

示例 5:

輸入:dictionary = ["ac","ab"], sentence = "it is abnormal that this solution is accepted"
輸出:"it is ab that this solution is ac"

提示:

  • 1 <= dictionary.length<= 1000
  • 1 <= dictionary[i].length <= 100
  • dictionary[i]僅由小寫字母組成。
  • 1 <= sentence.length <= 10^6
  • sentence僅由小寫字母和空格組成。
  • sentence 中單詞的總量在範圍 [1, 1000] 內。
  • sentence 中每個單詞的長度在範圍 [1, 1000] 內。
  • sentence 中單詞之間由一個空格隔開。
  • sentence沒有前導或尾隨空格。

注意:本題與主站 648題相同:

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," ")



}
​