public static void KMP ( String text , String pattern ) {
  int [ ] lps = LPS ( pattern ) ;
  int i = 0 ;
  int j = 0 ;
  ArrayList < Integer > matches = new ArrayList < > ( ) ;
  while ( i < text . length ( ) ) {
    if ( text . charAt ( i ) == pattern . charAt ( j ) ) {
      i ++ ;
      j ++ ;
    }
    else {
      if ( j != 0 ) {
        j = lps [ j - 1 ] ;
      }
      else {
        i ++ ;
      }
    }
    if ( j == pattern . length ( ) ) {
      matches . add ( i - j ) ;
      j = lps [ j - 1 ] ;
    }
  }
  for ( int x : matches ) {
    System . out . println ( "Match at : " + x ) ;
  }
}

public static void KMP ( String text , String pattern ) { int [ ] lps = LPS ( pattern ) ; int i = 0 ; int j = 0 ; ArrayList < Integer > matches = new ArrayList < > ( ) ; while ( i < text . length ( ) ) { if ( text . charAt ( i ) == pattern . charAt ( j ) ) { i ++ ; j ++ ; } else { if ( j != 0 ) { j = lps [ j - 1 ] ; } else { i ++ ; } } if ( j == pattern . length ( ) ) { matches . add ( i - j ) ; j = lps [ j - 1 ] ; } } for ( int x : matches ) { System . out . println ( " Match ▁ at ▁ : ▁ " + x ) ; } }
(method_declaration (modifiers) type: (void_type) name: (identifier) parameters: (formal_parameters (formal_parameter type: (type_identifier) name: (identifier)) (formal_parameter type: (type_identifier) name: (identifier))) body: (block (local_variable_declaration type: (array_type element: (integral_type) dimensions: (dimensions)) declarator: (variable_declarator name: (identifier) value: (method_invocation name: (identifier) arguments: (argument_list (identifier))))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) (local_variable_declaration type: (generic_type (type_identifier) (type_arguments (type_identifier))) declarator: (variable_declarator name: (identifier) value: (object_creation_expression type: (generic_type (type_identifier) (type_arguments)) arguments: (argument_list)))) (while_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) body: (block (if_statement condition: (parenthesized_expression (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier))) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier))))) consequence: (block (expression_statement (update_expression (identifier))) (expression_statement (update_expression (identifier)))) alternative: (block (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (decimal_integer_literal))) consequence: (block (expression_statement (assignment_expression left: (identifier) right: (array_access array: (identifier) index: (binary_expression left: (identifier) right: (decimal_integer_literal)))))) alternative: (block (expression_statement (update_expression (identifier))))))) (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) consequence: (block (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (binary_expression left: (identifier) right: (identifier))))) (expression_statement (assignment_expression left: (identifier) right: (array_access array: (identifier) index: (binary_expression left: (identifier) right: (decimal_integer_literal))))))))) (enhanced_for_statement type: (integral_type) name: (identifier) value: (identifier) body: (block (expression_statement (method_invocation object: (field_access object: (identifier) field: (identifier)) name: (identifier) arguments: (argument_list (binary_expression left: (string_literal) right: (identifier)))))))))