题目链接:
力扣https://leetcode-cn.com/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/
【分析】先用split划分字符串,然后就转换成了单词的前缀匹配问题了,可以直接用startsWith或者用字典树。
startsWith
class Solution {
public int isPrefixOfWord(String sentence, String searchWord) {
String[] strs = sentence.split(" ");
int n = strs.length, i;
for(i = 0; i < n; i ++){
if(strs[i].startsWith(searchWord)) return i + 1;
}
return -1;
}
}