1455. 检查单词是否为句中其他单词的前缀
JS代码:js就很便捷!startsWith()
/**
* @param {string} sentence
* @param {string} searchWord
* @return {number}
*/
var isPrefixOfWord = function(sentence, searchWord) {
let arr = sentence.split(' ');
for (let i = 0; i < arr.length; ++i) {
if (arr[i].startsWith(searchWord)) {
return i + 1;
}
}
return -1;
};
Java代码:
在这里插入代码片