Skip to content

Instantly share code, notes, and snippets.

@RitamChakraborty
Created August 11, 2019 17:17
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 RitamChakraborty/fb0cbae39ee91f259263f9dfb0706804 to your computer and use it in GitHub Desktop.
Save RitamChakraborty/fb0cbae39ee91f259263f9dfb0706804 to your computer and use it in GitHub Desktop.
Basic solution for string matching algorithm.
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