Files
leetcode/28.find-the-index-of-the-first-occurrence-in-a-string.java
2025-10-23 10:33:53 +08:00

25 lines
594 B
Java

/*
* @lc app=leetcode id=28 lang=java
*
* [28] Find the Index of the First Occurrence in a String
*/
// @lc code=start
class Solution {
public int strStr(String haystack, String needle) {
int idx = -1;
if(haystack.length() < needle.length()) {
return idx;
}
for(int i = 0; i < haystack.length(); i++) {
if(i + needle.length() <= haystack.length() && haystack.substring(i, i + needle.length()).equals(needle)) {
idx = i;
break;
}
}
return idx;
}
}
// @lc code=end