Implement strStr ()-leetcode-java

Source: Internet
Author: User

"The original in the SAE's blog, all transferred to the CSDN. "
Implement strStr ()-leetcode-javaPosted in2016/02/06

Test instructions

Implement strStr ().

Returns the index of the first occurrence of needle in haystack, or-1 if needle are not part of haystack.

String matching problem, if the substring exists in the main string, then returns the position of the first character that the substring first appeared in the main string, or 1 if no one exists.

Idea: traverse the length of the main string, if the position of I and the length of the substring has exceeded the length of the main string, the description does not exist, returns-1. When the substring starts to match, the position of I as the first character in order to preserve the match succeeds, increments with M. Compare if each bit of the substring and the main string is the same, and when J equals the length of the substring minus one, the match proves successful.

public class Solution {
public int strStr (String haystack, string needle) {
if (Haystack==null | | needle==null) return 0;
if (Needle.length () ==0) return 0;
for (int i=0;iif (I+needle.length () >haystack.length ()) return-1;
int m=i;
for (int j=0;j<needle.length (); j + +) {
if (Needle.charat (j) ==haystack.charat (m)) {
if (J==needle.length ()-1)
{return i;}
m++;
}else break;
}
}
return-1;
}}

The following method is also widely circulated and easy to understand:

public int strStr (String haystack, string needle) {

Starting from Heystack

for (int i = 0;; i++) {

Match needle characters

for (int j = 0;; j + +) {

If Needle and J are the same length, then I (the starting position of the current match) is returned directly, because the match is successful.

if (j = = Needle.length ()) {return i;}

If I+j is the length of the current haystack, it indicates that all characters of heystack have been completed and that there is no successful match (note that if the last character matches successfully, a judgement will be returned).

if (i + j = = Haystack.length ()) {return-1;}

If the current needle and haystack characters are the same (because each unsuccessful match is unsuccessful, I moves 1 bits, and J starts again from 0, so the current position of Haystack is i+j)

if (Needle.charat (j)! = Haystack.charat (i + j)) {Break;}} } }

This question also has the KMP solution, OH this big Boss, the light understanding its thought I already used up today's brain power ...

High-handwritten KMP solution:

public int strStr (String haystack, string needle) {if (Haystack==null | | needle==null) return 0;int h = haystack.length (), int n = needle.length (), if (n > H) return-1;if (n = = 0) return 0;int[] next = getNext (needle); int i = 0;while (i <= h-n) {int success = 1;for (int j = 0; J < N; j + +) {if (Needle.charat (0)! = Haystack.charat (i)) { Success = 0;i++;break;} else if (Needle.charat (j)! = Haystack.charat (i + j)) {success = 0;i = i + J-next[j-1];break;}} if (success = = 1) return i;} return-1;} Calculate KMP arraypublic int[] GetNext (String needle) {int[] next = new Int[needle.length ()];next[0] = 0;for (int i = 1 ; I < needle.length ();  i++) {int index = next[i-1];while (Index > 0 && needle.charat (index)! = Needle.charat (i)) {index = Next[index -1];} if (Needle.charat (index) = = Needle.charat (i)) {next[i] = next[i-1] + 1;} else {next[i] = 0;}} return next;}
After taking some time to understand the idea, the understanding of the next array put me on the halfway, but also a variety of search, search to PKU of an ACM PPT I point, and the previous blog explained in series together:
Here's an example to illustrate the running process,
?     i=123456789......?     a=abababaabab...?     b =        A b A B a C b?    J =&NBSP;&NBSP;&NBSP;&N bsp;     1 2 3 4 5 6 7 when i=6,j=5, A[i+1]!=b[j+1], so j=next[5]=3?     i = 1 2 3 4 5 6 7 8 9 ...?     a=abababaabab...?     B =                     a B A B a C b?    J =                      1 2 3 4 5 6 7? At this time i=6,j=1  still does not meet a[i+1]==b[j+1], So continue to reduce j, so that j=next[1]=0?     i=123456789......?     a=abababaabab...?     B =                         a B A B a C b?    J =                          1 2 3 4 5 6 7? Finally, A[8]=b[1],i becomes 8, J for 1?     i=123456789......?     a=abababadbab...?     B =                         a B A B a C b?    J =                           1 2 3 4 5 6 7? In fact, it is possible that J to 0 still cannot satisfy a[i+1]=b[j+1] (such as a[8]= "D"). Therefore, it is accurate to say that, when j=0, we directly increase the I value but ignore J until A[i]=b[1] is present. PS: There is a little doubt, above where I is the value of the wrong, such as the top one, I value of 7,j=5 time it? But does not affect the overall understanding)
I think the article that helps to understand KMP thought:
http://blog.csdn.net/yutianzuijin/article/details/11954939
This article at the end of the author returned to a Koch curve, I started really do not understand why, understand and then understand, the author would like to express a local part of the idea, the substring is the pattern string continuous shortening, there is also a matching prefix and suffix.
Http://www.cnblogs.com/goagent/archive/2013/05/16/3068442.html
This article about next formula and diagram is very good
http://blog.csdn.net/power721/article/details/6132380
This article describes the matching process in the form of a table, which is also quite intuitive.
Others can refer to the following text:
Http://www.cppblog.com/oosky/archive/2006/07/06/9486.html
http://blog.csdn.net/joylnwang/article/details/6778316
Posted in leetcode|  tags are KMP, Leetcode, Algorithms| Post a reply

Implement strStr ()-leetcode-java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.