-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
30 lines (29 loc) · 788 Bytes
/
Copy pathSolution.java
File metadata and controls
30 lines (29 loc) · 788 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class Solution {
public int strStr(String haystack, String needle) {
if (needle.isEmpty()) {
return 0;
}
return haystack.indexOf(needle);
}
}
/*
* class Solution {
* public int strStr(String haystack, String needle) {
* // If needle is an empty string, return 0 (according to problem constraints)
* if (needle.length() == 0) {
* return 0;
* }
*
* // Iterate through the haystack and check for substring match
* for (int i = 0; i <= haystack.length() - needle.length(); i++) {
* // Check if the substring of haystack starting at index i matches needle
* if (haystack.substring(i, i + needle.length()).equals(needle)) {
* return i; // Found the first occurrence
* }
* }
*
* return -1; // No match found
* }
* }
*
*/