Skip to content

Instantly share code, notes, and snippets.

@prashanth-g
Created January 13, 2023 06:28
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 prashanth-g/e28eb4479be95ea3cc71b4a9c4fe926c to your computer and use it in GitHub Desktop.
Save prashanth-g/e28eb4479be95ea3cc71b4a9c4fe926c to your computer and use it in GitHub Desktop.
Pattern matching in O(n * m)
package com.prashanth;
public class StringMatching {
public static void main(String[] args) {
String pattern = "eeccd";
String word = "cceeccd";
int k = 0;
for (int i = 0; i < word.length(); i++) {
int l = i;
for (int j = k; j < pattern.length();) {
if (word.charAt(l) == pattern.charAt(j)) {
j++;
k++;
l++;
} else {
j=0;
k=0;
break;
}
}
}
if (pattern.length() == k) {
System.out.println("Pattern found in the given text");
} else {
System.out.println("Pattern not found in the given text");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment