Skip to content

Commit 713bc59

Browse files
committed
add: initial implementation of Rabin-Karp algorithm for string matching
1 parent 4b09fab commit 713bc59

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

.clang-format

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ IndentGotoLabels: true
8383
IndentPPDirectives: None
8484
IndentWidth: 4
8585
IndentWrappedFunctionNames: false
86-
InsertNewlineAtEOF: true
8786
JavaScriptQuotes: Leave
8887
JavaScriptWrapImports: true
8988
KeepEmptyLinesAtTheStartOfBlocks: true

src/main/java/com/thealgorithms/strings/PatternSearchUsingRabinKarpAlgo.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ public static List<String> search(String text, String pattern) {
1515
List<String> result = new ArrayList<>();
1616
int m = pattern.length();
1717
int n = text.length();
18-
int prime = 101; // A prime number to mod hash values
18+
int prime = 101; // A prime number to mod hash values
1919

20-
int patternHash = 0; // Hash value for pattern
21-
int textHash = 0; // Hash value for text window
20+
int patternHash = 0; // Hash value for pattern
21+
int textHash = 0; // Hash value for text window
2222
int h = 1;
2323

2424
// The value of h would be "pow(d, m-1) % prime"
@@ -69,11 +69,11 @@ public static void main(String[] args) {
6969
Scanner in = new Scanner(System.in);
7070
System.out.print("Enter the string: ");
7171
String text = in.next();
72-
// String text = "ABCCDDAEFG"; test
72+
// String text = "ABCCDDAEFG"; test
7373

7474
System.out.print("Enter the searching string: ");
7575
String pattern = in.next();
76-
// String pattern = "CDD"; testt
76+
// String pattern = "CDD"; testt
7777

7878
List<String> result = search(text.toLowerCase(), pattern.toLowerCase());
7979

@@ -82,5 +82,7 @@ public static void main(String[] args) {
8282
} else {
8383
System.out.println("Pattern found: " + result);
8484
}
85+
86+
in.close();
8587
}
8688
}

0 commit comments

Comments
 (0)