package com.算法专练.力扣.检查单词是否为句中其他单词的前缀;
/**
* @author xnl
* @Description:
* @date: 2022/8/21 21:12
*/
public class Solution {
public static void main(String[] args) {
Solution solution = new Solution();
String sentence = "i love eating burger", searchWord = "burg";
System.out.println(solution.isPrefixOfWord(sentence, searchWord));
}
/**
* 暴力
* @param sentence
* @param searchWord
* @return
*/
public int isPrefixOfWord(String sentence, String searchWord) {
int ans = -1;
String[] split = sentence.split("\\s+");
for (int i = 0; i < split.length; i++){
int count = 0;
for (int j = 0; j < searchWord.length() && j < split[i].length() ; j++){
if (split[i].charAt(j) == searchWord.charAt(j)){
count++;
}
}
if (count == searchWord.length()){
ans = i +1;
break;
}
}
return ans;
}
}