/** * Brute force matching for Pattern Matching */ Package javay. util; /** * Pattern Match Brute-Force * @ Author DBJ */ Public class PMBF { /** * Pattern Match Brute-Force * @ Param target string * @ Param pattern string * @ Return refers to the position of the first occurrence of a string in the target string. */ Public static int patternMatch (String target, String pattern ){ Int targetLength = target. length (); Int patternLength = pattern. length (); Int idxTgt = 0; // character position in the target string Int idxPtn = 0; // character position in the mode string Int index = 0; // Save the position of the starting character that matches the ing with the mode string While (idxTgt <targetLength & idxPtn <patternLength ){ // Find a matched character If (target. charAt (idxTgt) = pattern. charAt (idxPtn )){ // If the characters are equal, the subsequent comparison of the characters will continue. IdxTgt ++; IdxPtn ++; } Else { // Otherwise, the target string starts from the second character and recompares with the first character of the pattern string. Index ++; IdxPtn = 0; IdxTgt = index; } } // Match to one and output the result If (idxPtn = patternLength ){ // Indicates that the matching is successful. Return index; } Else { Return-1; } } } |