strStr
If target does not exist in source, just return-1
.
Do I need to implement KMP Algorithm in a real interview?
Not necessary. When you meet this problem in a real interview, the interviewer may just want to test your basic implementation ability. But make sure your confirm with the interviewer first.
Example
If source ="source"
and target ="target"
, return-1
.
If source ="abcdabcdefg"
and target ="bcd"
, return1
.
分析
起点从i开始Loop,注意边界i到source.length() - target.length() + 1。j是长度。当j达到target长度就找到了起点i。
答案
python做法
需要保持一个fixed sliding window的长度为短字符串的长度然后扫长字符串来寻找起始位置
难点还是index
Last updated
Was this helpful?