Created
January 13, 2023 06:28
-
-
Save prashanth-g/e28eb4479be95ea3cc71b4a9c4fe926c to your computer and use it in GitHub Desktop.
Pattern matching in O(n * m)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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