Check If a Word Occurs As a Prefix of Any Word in a Sentence
给一个sentence和一个searchword, 问searchword是sentence中第几个word的前缀字符串.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class Solution { public int isPrefixOfWord(String sentence, String searchWord) { String[] strs = sentence.split(" "); int i = 0; for(String s : strs) { if(s.startsWith(searchWord)){ return i+1; } i++; } return -1; } } |