leetcode 648. 单词替换

题解

判断单词是否有词根前缀

示例代码(go)

1
2
3
4
5
6
7
8
9
10
11
func replaceWords(dict []string, sentence string) string {
arr := strings.Split(sentence, " ")
for i := 0; i < len(arr); i++ {
for _, root := range dict {
if strings.HasPrefix(arr[i], root) {
arr[i] = root
}
}
}
return strings.Join(arr, " ")
}