Skip to content

Commit 00e9f44

Browse files
refactor 28
1 parent 1c8bb49 commit 00e9f44

File tree

2 files changed

+47
-23
lines changed

2 files changed

+47
-23
lines changed
Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,26 @@
11
package com.fishercoder.solutions;
2-
/**Implement strStr().
3-
4-
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
5-
2+
/**
3+
* 28. Implement strStr()
4+
*
5+
* Implement strStr().
6+
*
7+
* Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
68
*/
79
public class _28 {
8-
/**You could use substring as follows, or use two pointers to go through the haystack, if substring API call is not allowed.*/
9-
public static int strStr(String haystack, String needle) {
10-
if (haystack == null || needle == null || haystack.length() < needle.length()) {
10+
11+
public static class Solution1 {
12+
public int strStr(String haystack, String needle) {
13+
if (haystack == null || needle == null || haystack.length() < needle.length()) {
1114
return -1;
12-
}
15+
}
1316

14-
for (int i = 0; i <= haystack.length() - needle.length(); i++) {
17+
for (int i = 0; i <= haystack.length() - needle.length(); i++) {
1518
if (haystack.substring(i, i + needle.length()).equals(needle)) {
16-
return i;
19+
return i;
1720
}
21+
}
22+
return -1;
1823
}
19-
return -1;
20-
}
21-
22-
public static void main(String... args) {
23-
// String haystack = "a";
24-
// String needle = "";
24+
}
2525

26-
// String haystack = "mississippi";
27-
// String needle = "a";
28-
29-
String haystack = "a";
30-
String needle = "a";
31-
strStr(haystack, needle);
32-
}
3326
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._28;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
7+
import static junit.framework.TestCase.assertEquals;
8+
9+
public class _28Test {
10+
private static _28.Solution1 solution1;
11+
12+
@Before
13+
public void setupForEachTest() {
14+
solution1 = new _28.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(0, solution1.strStr("a", ""));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(-1, solution1.strStr("mississippi", "a"));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(0, solution1.strStr("a", "a"));
30+
}
31+
}

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