strStr
For a given source string and a target string, you should output the first index(from 0) of target string in source string.
If target does not exist in source, just return -1.
Example
If source = “source” and target = “target”, return -1.
If source = “abcdabcdefg” and target = “bcd”, return 1.
Challenge
O(n2) is acceptable. Can you implement an O(n) algorithm? (hint: KMP)
Solution
Be careful about the corner cases. (e.g. NULL, emptry string)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|