This question is merely about algorithm. In pseudo code is like this:
A = Array of strings; //let's say count(A) = N
S = String to find; //let's say length(S) = M
for (Index=0; Index<count(A); Index++)
if (A[Index]==S) {
print "First occurrence at index\x20"+Index;
break;
}
This for loop requires string comparison N times (or byte comparison N*M times, O(N*M)). This is bad when array A has lots of items, or when string S is too long.
Any better method to find out the first occurrence? Some algorithm at O(K*logK) is OK, but preferable at O(K) or best at O(logK), where K is either N or M.
I don't mind adding in some other structures or doing some data processing before the comparison loop.
Awith the same length and an identical long prefix. (String equality checks can terminate immediately if the lengths are different, or a soon as a mismatch is found in walking over them.)\x20instead of a space ? I'm curious :-)