Created
August 8, 2014 00:46
-
-
Save KodeSeeker/3977f5e0651a391d611c to your computer and use it in GitHub Desktop.
Pattern Search : Naive
This file contains hidden or 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
| /** | |
| * Given a text txt[0..n-1] and a pattern pat[0..m-1], write a function search(char pat[], char txt[]) | |
| * that prints all occurrences of pat[] in txt[]. You may assume that n > m. | |
| * E.g. | |
| * txt= "THIS IS A TEST TEXT" | |
| * pat = "TEST" | |
| Output : Pattern found at index 10 | |
| */ | |
| public int findPattern(String txt, String pat) | |
| { | |
| if (txt== null ) | |
| return -1; | |
| if (pat==null) | |
| return -1; | |
| char[] txtChar = txt.tocharArray(); | |
| char[] patchar=pat.tocharArray(); | |
| //Algo: move pattern over the search text in a window. | |
| for(int i=0;i< txtChar.length-patChar.length; ) | |
| { | |
| int j=0; | |
| for(j<patChar.length;j++) | |
| { | |
| if(txtChar[i+j]!=patChar[i+j]) | |
| break; | |
| } | |
| if(j==patChar.length) | |
| { | |
| Sytem.out.println("MAtch found at"+i ); | |
| break; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment