All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0001_0100.s0028_implement_strstr.Solution Maven / Gradle / Ivy

There is a newer version: 1.24
Show newest version
package g0001_0100.s0028_implement_strstr;

// #Easy #Top_Interview_Questions #String #Two_Pointers #String_Matching
// #Programming_Skills_II_Day_1 #2022_06_14_Time_0_ms_(100.00%)_Space_40.4_MB_(83.88%)

public class Solution {
    public int strStr(String haystack, String needle) {
        if (needle.isEmpty()) {
            return 0;
        }
        int m = haystack.length();
        int n = needle.length();
        for (int start = 0; start < m - n + 1; start++) {
            if (haystack.substring(start, start + n).equals(needle)) {
                return start;
            }
        }
        return -1;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy