Skip to content

Commit 4c62aa0

Browse files
authored
Merge pull request kodecocodes#664 from jawwad/rabin-karp-updates
Rabin karp updates
2 parents 2ea681c + 7e75d10 commit 4c62aa0

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

Rabin-Karp/README.markdown

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Rabin-Karp string search algorithm
22

3-
The Rabin-Karp string search alogrithm is used to search text for a pattern.
3+
The Rabin-Karp string search algorithm is used to search text for a pattern.
44

55
A practical application of the algorithm is detecting plagiarism. Given source material, the algorithm can rapidly search through a paper for instances of sentences from the source material, ignoring details such as case and punctuation. Because of the abundance of the sought strings, single-string searching algorithms are impractical.
66

@@ -12,10 +12,10 @@ at a time (e.g. "he ") and subtracts out the previous hash from the "T".
1212

1313
## Algorithm
1414

15-
The Rabin-Karp alogrithm uses a sliding window the size of the search pattern. It starts by hashing the search pattern, then
15+
The Rabin-Karp algorithm uses a sliding window the size of the search pattern. It starts by hashing the search pattern, then
1616
hashing the first x characters of the text string where x is the length of the search pattern. It then slides the window one character over and uses
1717
the previous hash value to calculate the new hash faster. Only when it finds a hash that matches the hash of the search pattern will it compare
18-
the two strings it see if they are the same (prevent a hash collision from producing a false positive)
18+
the two strings it see if they are the same (to prevent a hash collision from producing a false positive).
1919

2020
## The code
2121

@@ -24,8 +24,8 @@ The major search method is next. More implementation details are in rabin-karp.
2424
```swift
2525
public func search(text: String , pattern: String) -> Int {
2626
// convert to array of ints
27-
let patternArray = pattern.characters.flatMap { $0.asInt }
28-
let textArray = text.characters.flatMap { $0.asInt }
27+
let patternArray = pattern.flatMap { $0.asInt }
28+
let textArray = text.flatMap { $0.asInt }
2929

3030
if textArray.count < patternArray.count {
3131
return -1
@@ -37,7 +37,7 @@ public func search(text: String , pattern: String) -> Int {
3737
let firstHash = hash(array: firstChars)
3838

3939
if (patternHash == firstHash) {
40-
// Verify this was not a hash collison
40+
// Verify this was not a hash collision
4141
if firstChars == patternArray {
4242
return 0
4343
}
@@ -76,4 +76,4 @@ This will return 13 since ump is in the 13 position of the zero based string.
7676
[Rabin-Karp Wikipedia](https://en.wikipedia.org/wiki/Rabin%E2%80%93Karp_algorithm)
7777
7878
79-
*Written by [Bill Barbour](https://github.com/brbatwork)*
79+
*Written by [Bill Barbour](https://github.com/brbatwork)*

Rabin-Karp/Rabin-Karp.playground/Contents.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ extension Character {
3030
// Find first position of pattern in the text using Rabin Karp algorithm
3131
public func search(text: String, pattern: String) -> Int {
3232
// convert to array of ints
33-
let patternArray = pattern.characters.flatMap { $0.asInt }
34-
let textArray = text.characters.flatMap { $0.asInt }
33+
let patternArray = pattern.flatMap { $0.asInt }
34+
let textArray = text.flatMap { $0.asInt }
3535

3636
if textArray.count < patternArray.count {
3737
return -1

0 commit comments

Comments
 (0)