Skip to content

Commit 3ca9bdc

Browse files
authored
Merge pull request neetcode-gh#2331 from benmak11/0028-find-index-of-first-occurence-in-string
Create 0028-find-the-index-of-the-first-occurence-in-a-string
2 parents 48eed3a + 3836ac0 commit 3ca9bdc

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

java/0028-find-the-index-of-the-first-occurrence-in-a-string.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Solution One
12
public class Solution {
23
public int strStr(String haystack, String needle) {
34
if (needle.isEmpty()) {
@@ -40,3 +41,17 @@ public int strStr(String haystack, String needle) {
4041
}
4142
}
4243

44+
// Solution Two (Linear search using indexOf method for arrays)
45+
class Solution {
46+
public int strStr(String haystack, String needle) {
47+
int result = 0;
48+
if (haystack.length() <= 0 && needle.length() > 0) return -1;
49+
if (haystack.length() != 0) {
50+
int occurence = haystack.indexOf(needle);
51+
if (occurence == -1)
52+
return occurence;
53+
result = occurence;
54+
}
55+
return result;
56+
}
57+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy