Skip to content

Instantly share code, notes, and snippets.

@john-nash-rs
Created February 10, 2019 06:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save john-nash-rs/ef8e116b0772c7cc894f584df577eb42 to your computer and use it in GitHub Desktop.
Save john-nash-rs/ef8e116b0772c7cc894f584df577eb42 to your computer and use it in GitHub Desktop.
import java.util.ArrayList;
import java.util.List;
public class StringMatchBruteForce {
public static void main (String args []){
System.out.println("========String matcher Brute Force Approach===========");
String text = "abcabbabbaabac";
String pattern = "abba";
long initialTime = System.nanoTime();
List<Integer> matchedIndexes = bruteForceStringMatcher(text, pattern);
long finalTime = System.nanoTime();
for(Integer matchedIndex : matchedIndexes){
System.out.println("Pattern found at "+matchedIndex);
}
if(matchedIndexes.size() == 0)
System.out.println("Pattern not found");
System.out.println("Time taken for matching "+(finalTime - initialTime));
}
public static List<Integer> bruteForceStringMatcher(String text, String pattern){
char[] textArray = text.toCharArray();
char[] patternArray = pattern.toCharArray();
int textLength = textArray.length;
int patternLength = patternArray.length;
List<Integer> matchedIndexes = new ArrayList<>();
int textIndex = 0;
for(textIndex = 0; textIndex < textLength; textIndex++){
int textIndexLocal = textIndex;
Boolean match = true;
int matchedIndex = textIndex;
for(int patternIndex = 0; patternIndex < patternLength; patternIndex++){
if(textArray[textIndexLocal] != patternArray[patternIndex]) {
match = false;
break;
}
textIndexLocal = textIndexLocal + 1;
}
if(match)
matchedIndexes.add(matchedIndex);
}
return matchedIndexes;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment