As reported: Adobe SDE-2 (C++) loop (2025), round 2: KMP string matching — a brute-force and a better approach, the better one coded, with time and space complexities.
This is LeetCode 28 — Find the Index of the First Occurrence in a String.
Return the index of the first occurrence of needle in haystack, or -1 if it does not occur. Start with the brute force, then do it in linear time with the Knuth–Morris–Pratt algorithm.
Example 1
haystack = "sadbutsad", needle = "sad"
Output: 0
Example 2
haystack = "leetcode", needle = "leeto"
Output: -1
Constraints: 1 ≤ haystack.length, needle.length ≤ 10^4; both are lowercase English letters.
Code runner format: one JSON value per line: haystack, needle. Implement strStr(haystack, needle); the harness prints the index.