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;
}
}
力扣:1455. 检查单词是否为句中其他单词的前缀
最新推荐文章于 2025-11-24 01:23:29 发布
本文档介绍了一种算法实现,用于判断给定单词是否为句子中其他单词的前缀。通过暴力搜索和字符串分割,'Solution' 类提供了 isPrefixOfWord 方法进行高效查找。

279

被折叠的 条评论
为什么被折叠?



