Substring search: given a piece of text with a length of N and a pattern string with a length of M, find a substring that matches the pattern in the text.
Although the worst-case running time is proportional to M * n, most of the brute-force algorithms do not match when the first character is compared, the actual running time is generally proportional to m + n.
The following is the JAVA Implementation of the brute force substring search algorithm:
/*** Find the brute-force string. If it is found, return the position where Pat appears for the first time in the TXT file; if n is not found, the return value * @ Param TXT * @ Param Pat * @ return */Public int strreplace (string txt, string Pat) {int n = TXT. length (); int M = pat. length (); For (INT I = 0; I <n; I ++) {Int J; For (j = 0; j <m; j ++) {If (txt. charat (I + J )! = Pat. charat (j) {break ;}} if (j = m) {return I ;}} return n ;}