Given two strings needle and haystack, return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

Input: haystack = "sadbutsad", needle = "sad"
Output: 0
Explanation: "sad" occurs at index 0 and 6. The first occurrence is at index 0, so we return 0.
Input: haystack = "cscodeio", needle = "cscd"
Output: -1
Explanation: "cscd" did not occur in "cscodeio", so we return -1.
Constraints:

Contents

In this approach, we will use two pointers i to point to input string haystack and j to point at needle, and find the starting matching character from needle in haystack, then keep checking if rest of the characters are matching, if they match, return the index where the match found, otherwise keep moving i pointer until the next match found and repeat this until we went through all characters of haystack string.

implementation steps:
public class IndexOfFirstOccurrenceInAString { static int indexOf(String haystack, String needle) { int i =0; while(i < haystack.length() && i+needle.length() <= haystack.length()) { int j=0; for(; j<needle.length(); j++) { if(needle.charAt(j) != haystack.charAt(i+j)) { // not same characters break; } } if(j == needle.length()) { return i; } i++; } return -1; } }
Complexity Analysis:

Time complexity: Above code runs in O(n * m) time where n is the length of input string haystack and m is the length of input string needle.
Space complexity: O(1)

In this approach, we will apply KMP (Knuth-Morris-Pratt) algorithm to match a pattern pattern in a string text.

The main idea in this alogorithm is, in the naive approach, say we started matching characters from index i and when a character mismatch found at an index j between the pattern and text, we will start again from i+1 from where it started, and this takens O (n * m) time complexity. But, KMP algorithm helps not to start from beginning again and instead start from where few starting characters can be ignored, thus by improving the runtime complexity to O(n).

For example, say text = "aaaab" and pattern = "aaab", when we start matching characters, we will find a mismatch at 4th index between text = "aaaab" and pattern = "aaab", then we have to start again from 2nd character in text = "aaaab", this is where KMP algorithm helps.

Implementation steps:
public class IndexOfFirstOccurrenceInAString { /** * Pattern matching using KMP algorithm * 1. build a LPS array (longest prefix suffix array) * 2. match characters between haystack and needle * if any mismatch found, then reset needle index to a prefix index from LPS array * */ static int patternMatchingUsingKMP(String haystack, String needle) { int[] lps = createLPSArray(needle); int i=0; // track haystack int j=0; // track needle while(i<haystack.length() && j<needle.length()) { if(haystack.charAt(i) == needle.charAt(j)) { i++; j++; } else { if(j != 0) { j = lps[j-1]; } else { i++; } } if(j == needle.length()) { return i-j; } } return -1; } static int[] createLPSArray(String pattern) { int[] lps = new int[pattern.length()]; int i=0; int j=1; // start from index 1, because at index 0, there is no character (previous) to compare with while(j < pattern.length()) { if(pattern.charAt(i) == pattern.charAt(j)) { lps[j] = i +1; i++; j++; } else { if (i != 0) { i = lps[i-1]; } else { lps[j] = 0; j++; } } } return lps; } }
Complexity Analysis:

Time complexity: Above code runs in O(n) time where n is the length of input string haystack, though we are building LPS array iterating through needle string, length of this will not be greater than n.
Space complexity: O(m) where m is the length of input string needle.

Above implementations source code can be found at GitHub link for Java code