From 57ed447cb0a468876653fffbaf9e26f04ab5b006 Mon Sep 17 00:00:00 2001 From: ashmichheda Date: Sat, 11 Nov 2023 18:11:25 -0800 Subject: [PATCH 1/2] feat: add solution to problem 28 --- src/main/java/com/fishercoder/solutions/_28.java | 16 ++++++++++++++++ src/test/java/com/fishercoder/_28Test.java | 5 +++++ 2 files changed, 21 insertions(+) diff --git a/src/main/java/com/fishercoder/solutions/_28.java b/src/main/java/com/fishercoder/solutions/_28.java index 582899b0dc..9c656a706f 100644 --- a/src/main/java/com/fishercoder/solutions/_28.java +++ b/src/main/java/com/fishercoder/solutions/_28.java @@ -17,4 +17,20 @@ public int strStr(String haystack, String needle) { } } + public static class Solution2 { + public int strStr(String haystack, String needle) { + + int n = needle.length(); + int h = haystack.length(); + + for(int i = 0; i <= h - n; i++) { + for(int j = 0; j < n && haystack.charAt(i + j) == needle.charAt(j); j++) { + if(j == n - 1) return i; + } + } + + return -1; + } + } + } diff --git a/src/test/java/com/fishercoder/_28Test.java b/src/test/java/com/fishercoder/_28Test.java index ce9378a6f2..22f42d6886 100644 --- a/src/test/java/com/fishercoder/_28Test.java +++ b/src/test/java/com/fishercoder/_28Test.java @@ -8,20 +8,25 @@ public class _28Test { private static _28.Solution1 solution1; + private static _28.Solution2 solution2; @Before public void setupForEachTest() { solution1 = new _28.Solution1(); + solution2 = new _28.Solution2(); } @Test public void test1() { assertEquals(0, solution1.strStr("a", "")); + assertEquals(0, solution2.strStr("sadbutsad", "sad")); } @Test public void test2() { + assertEquals(-1, solution1.strStr("mississippi", "a")); + assertEquals(8, solution2.strStr("leetcodea", "a")); } @Test From 1bb85ff662dbab702cf66d15a040bcc64de1bee9 Mon Sep 17 00:00:00 2001 From: ashmichheda Date: Sat, 11 Nov 2023 18:43:36 -0800 Subject: [PATCH 2/2] fix gradle build --- src/main/java/com/fishercoder/solutions/_28.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/fishercoder/solutions/_28.java b/src/main/java/com/fishercoder/solutions/_28.java index 9c656a706f..8e0323ad32 100644 --- a/src/main/java/com/fishercoder/solutions/_28.java +++ b/src/main/java/com/fishercoder/solutions/_28.java @@ -23,12 +23,13 @@ public int strStr(String haystack, String needle) { int n = needle.length(); int h = haystack.length(); - for(int i = 0; i <= h - n; i++) { - for(int j = 0; j < n && haystack.charAt(i + j) == needle.charAt(j); j++) { - if(j == n - 1) return i; + for (int i = 0; i <= h - n; i++) { + for (int j = 0; j < n && haystack.charAt(i + j) == needle.charAt(j); j++) { + if (j == n - 1) { + return i; + } } } - return -1; } } 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