Created
August 11, 2019 17:17
-
-
Save RitamChakraborty/fb0cbae39ee91f259263f9dfb0706804 to your computer and use it in GitHub Desktop.
Basic solution for string matching algorithm.
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
public class StringMatching { | |
private static boolean contains(String a, String b) { | |
int i = 0; | |
int n1 = a.length(); | |
int n2 = b.length(); | |
char[] ch1 = a.toCharArray(); | |
char[] ch2 = b.toCharArray(); | |
while (i < n1) { | |
int temp = i; | |
int j; | |
for (j = 0; j < n2; j++) { | |
if (ch1[i] == ch2[j]) { | |
i++; | |
} else { | |
i = temp + 1; | |
break; | |
} | |
} | |
if (j == n2) { | |
return true; | |
} | |
} | |
return false; | |
} | |
public static void main(String[] args) { | |
String a = "abcdeabdeabcdf"; | |
String b = "abcdf"; | |
System.out.println(contains(a, b)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment