refactor: change packages (#5430)

* refactor: change package

* refactor: fix name

---------

Co-authored-by: alxkm <alx@alx.com>
This commit is contained in:
Alex Klymenko
2024-08-30 11:58:24 +02:00
committed by GitHub
parent f8ff6af893
commit b0de93b3ce
10 changed files with 13 additions and 13 deletions

View File

@ -0,0 +1,60 @@
package com.thealgorithms.strings;
/**
* Implementation of KnuthMorrisPratt algorithm Usage: see the main function
* for an example
*/
public final class KMP {
private KMP() {
}
// a working example
public static void main(String[] args) {
final String haystack = "AAAAABAAABA"; // This is the full string
final String needle = "AAAA"; // This is the substring that we want to find
kmpMatcher(haystack, needle);
}
// find the starting index in string haystack[] that matches the search word P[]
public static void kmpMatcher(final String haystack, final String needle) {
final int m = haystack.length();
final int n = needle.length();
final int[] pi = computePrefixFunction(needle);
int q = 0;
for (int i = 0; i < m; i++) {
while (q > 0 && haystack.charAt(i) != needle.charAt(q)) {
q = pi[q - 1];
}
if (haystack.charAt(i) == needle.charAt(q)) {
q++;
}
if (q == n) {
System.out.println("Pattern starts: " + (i + 1 - n));
q = pi[q - 1];
}
}
}
// return the prefix function
private static int[] computePrefixFunction(final String p) {
final int n = p.length();
final int[] pi = new int[n];
pi[0] = 0;
int q = 0;
for (int i = 1; i < n; i++) {
while (q > 0 && p.charAt(q) != p.charAt(i)) {
q = pi[q - 1];
}
if (p.charAt(q) == p.charAt(i)) {
q++;
}
pi[i] = q;
}
return pi;
}
}

View File

@ -0,0 +1,82 @@
package com.thealgorithms.strings;
import java.util.Scanner;
/**
* @author Prateek Kumar Oraon (https://github.com/prateekKrOraon)
*
An implementation of Rabin-Karp string matching algorithm
Program will simply end if there is no match
*/
public final class RabinKarp {
private RabinKarp() {
}
public static Scanner scanner = null;
public static final int ALPHABET_SIZE = 256;
public static void main(String[] args) {
scanner = new Scanner(System.in);
System.out.println("Enter String");
String text = scanner.nextLine();
System.out.println("Enter pattern");
String pattern = scanner.nextLine();
int q = 101;
searchPat(text, pattern, q);
}
private static void searchPat(String text, String pattern, int q) {
int m = pattern.length();
int n = text.length();
int t = 0;
int p = 0;
int h = 1;
int j = 0;
int i = 0;
h = (int) Math.pow(ALPHABET_SIZE, m - 1) % q;
for (i = 0; i < m; i++) {
// hash value is calculated for each character and then added with the hash value of the
// next character for pattern as well as the text for length equal to the length of
// pattern
p = (ALPHABET_SIZE * p + pattern.charAt(i)) % q;
t = (ALPHABET_SIZE * t + text.charAt(i)) % q;
}
for (i = 0; i <= n - m; i++) {
// if the calculated hash value of the pattern and text matches then
// all the characters of the pattern is matched with the text of length equal to length
// of the pattern if all matches then pattern exist in string if not then the hash value
// of the first character of the text is subtracted and hash value of the next character
// after the end of the evaluated characters is added
if (p == t) {
// if hash value matches then the individual characters are matched
for (j = 0; j < m; j++) {
// if not matched then break out of the loop
if (text.charAt(i + j) != pattern.charAt(j)) {
break;
}
}
// if all characters are matched then pattern exist in the string
if (j == m) {
System.out.println("Pattern found at index " + i);
}
}
// if i<n-m then hash value of the first character of the text is subtracted and hash
// value of the next character after the end of the evaluated characters is added to get
// the hash value of the next window of characters in the text
if (i < n - m) {
t = (ALPHABET_SIZE * (t - text.charAt(i) * h) + text.charAt(i + m)) % q;
// if hash value becomes less than zero than q is added to make it positive
if (t < 0) {
t = (t + q);
}
}
}
}
}