Assume you have a function matchWithMagic that returns a Boolean value for a given string. You don’t know any detail how it do it but you know the result of true means “Match”. Assuming the complexity of this function is linear in time and space to the size of input string.
Now the question is to implement a function that for a given string, that will return an pair of integers, namely pos and len such that matchWithMagic(substring(inputstring,pos,len)) matches and pos is the least number that this can be true (earliest match). When pos is known, len is the least number for a match (shortest match, with a lower priority). There are no requirement on efficiency but the answer should includes performance analyse.
For example, suppose the magic function return true for input strings contains “Good Guy!” or “Bad Guy!” your function should return pos=5,len=8 for “Good Bad Guy!”.
Preferred languages is C/C++/Java/JavaScript/C#/Basic, but other languages are OK.
UPDATE
A trivial answer has now been posted. I hope a more efficient solution could appear.
Given that the function is black-box, I’m not sure you can do better than this:
Which I believe is
O(n^3)(assumingmatchWithMagicisO(n)).