Exact Pattern Matching: the brute force approach

• The Exact String Matching problem

• Exact String Matching:

• Given:

 A text string T = t0 t1 t2 ... tn-1                   length(T) = n A pattern string P = p0 p1 p2 ... pm-1          length(P) = m

• Problem:

 Determine all the occurences of the pattern P in the text T

• Example:

 ``` T = acgttagatactaggatacca P = gata Solution: acgttagatactaggatacca ```

• The brute force approach

• Reminder: the Brute Force method

 Generate every possible outcome           For each outcome, test if the outcome is a valid solution

• Example:

 ``` T = acgttagatactaggatgcca P = gata Try: acgttagatactaggatacca gata acgttagatactaggatacca gata acgttagatactaggatacca gata acgttagatactaggatacca gata and so on... ```

• What are the possible positions to try:

 ``` Positions: 0123456789..... T = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ppppp ^^^^^ pattern P may be found here ```

• pattern P may be found starting at position 0 of text string T
• pattern P may be found starting at position 1 of text string T
• ....

• Where do we start looking:

 ``` 0123456789..... T = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ppppp ^^^^^ First position that P could be found So: start looking for P at position: 0 ```

• Where do we stop looking:

 ``` 0123456789..... T = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ppppp ^^^^^ | Last position that P could be found What is this position: 0 T.length()-1 | | v v T = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |ppppp |^^^^^ | P.length() V position of x = T.length()-1-P.length() So: Last position to look for P = T.length()-P.length() ```

• Psuedo code for the brute force pattern matching algorithm:

 ``` T = text; P = pattern; for ( pos = 0; pos <= T.length()-P.length(); pos++ ) do { if ( P is found starting at position pos ) { print pos; } } ```

• Refined Psuedo code:

 ``` T = text; P = pattern; for ( pos = 0; pos <= T.length()-P.length(); pos++ ) do { cand = T.substring( pos, pos+P.length() ); // Try to match this... if ( P equal to cand ) { print pos; } } ```

• Comparing if 2 Strings are equal

• String is not a primitive type in Java

• String is a class (user defined type) provided by the Java library ( click here )

 ``` java.lang.String ```

• Operations on String variables are implemented with methods

• The method used to compare whether 2 Strings are equal ( click here ):

 ``` String1.equals( String2 ) returns: true if String1 and String2 contains the same characters in the same order false otherwise ```

• Example: check if the strings P and cand are equal

 ``` if ( P.equals(cand) ) { .... } ```

• Reading a line from the input

• Recall:

 ``` Scanner input = new Scanner( System.in ); String s; s = input.next(); // Read a word (ended by a space) ```

• Line (of text):

 Input line = a text that is ended by a new line (return) character (A line is a string, but it can contain space characters.)

• How to read a line from the input:

 ``` Scanner input = new Scanner( System.in ); String s; s = input.nextLine(); // Read a line (ended by RETURN) ```

• The exact string matching algorithm in Java

• Java code:

 ``` import java.util.Scanner; public class StringMatching1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); String T, P, cand; int pos; System.out.print("Enter a text string T: "); T = in.nextLine(); System.out.print("Enter a pattern string P: "); P = in.next(); for ( pos = 0; pos <= T.length() - P.length(); pos++ ) { cand = T.substring( pos, pos+P.length() ); // Try to match this... if ( P.equals(cand) ) { System.out.println("Found pattern at posistion: " + pos); // Found } } } } ```

• Example Program: (Demo above code)

How to run the program:

 Right click on link and save in a scratch directory To compile:   javac StringMatching1.java To run:          java StringMatching1

• Example output:

 ``` UNIX>> java StringMatching1 Enter a text string T: acgttagatactaggatacca Enter a pattern string P: gata 6 14 ```

• You can make the output prettier after you've done the hard work...

• Nice looking output:

 ``` import java.util.Scanner; public class StringMatching2 { public static void main(String[] args) { Scanner in = new Scanner(System.in); String T, P, cand; int pos, i; System.out.print("Enter a text string T: "); T = in.nextLine(); System.out.print("Enter a pattern string P: "); P = in.next(); System.out.println(); for ( pos = 0; pos <= T.length() - P.length(); pos++ ) { cand = T.substring( pos, pos+P.length() ); // Try to match this... if ( P.equals(cand) ) { System.out.println(T); // Print the text for ( i = 0 ; i < pos; i++ ) // Indent to found position System.out.print(" "); System.out.println(P); // Print the pattern below System.out.println(); } } } } ```

• Example Program: (Demo above code)

How to run the program:

 Right click on link and save in a scratch directory To compile:   javac StringMatching2.java To run:          java StringMatching2

Sample output:

 ``` UNIX>> java StringMatching2 Enter a text string T: acgttagatactaggatacca Enter a pattern string P: gata acgttagatactaggatacca gata acgttagatactaggatacca gata ```