You have a string (indexed from ) with no more than lowercase characters. Find the first occurrence of a string , or print -1
if is not a substring of .
Input Specification
The first line will have the string .
The second line will have the string .
Output Specification
Print the index of the first occurrence of the string in , or -1
if it is not a substring of .
Sample Input
higuyshowsitgoinghiguys
higuys
Sample Output
0
Comments
yo how u pass tle using java
The point value is admittedly slightly misleading thanks to the problem being very trivial in certain languages (e.g. C/++, Python 3).
You can either try making me sad by checking if Java has some built-in linear time string matching function, or implement KMP, Rabin-Karp, Z-algorithm, or any number of other linear-time string matching algorithms.
Is it possible to create a solution for test case # 15 using python 3? I can't seem to run it under the time limit.
Python's builtin
find
uses Boyer-Moore, which has a worst-case runtime of . Rabin-Karp and KMP are two algorithms which have a worst-case runtime of .This is incorrect; Boyer-Moore runs in linear time when the pattern does not occur in the text, which is not the case for Python.
To be pedantic: it's described as "a simplication of Boyer-Moore, incorporating ideas from Horspool and Sunday". The worst-case bound still holds. Whether you want to consider this "using Boyer-Moore" or not is up to you.
If you want to get more specific, the CPython implementation is described in their code as
Source: https://github.com/python/cpython/blob/main/Objects/stringlib/fastsearch.h
A challenge for anyone who's sufficiently bored would be figuring out if it is possible to force CPython to switch to the Two-Way algorithm on the given test data.