-
Notifications
You must be signed in to change notification settings - Fork 109
/
StringEqualsHashAlgorithm.kt
44 lines (39 loc) · 1.1 KB
/
StringEqualsHashAlgorithm.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package other
/**
*
* Algorithm for comparing two strings with a hash
*
*/
class StringEqualsHashAlgorithm {
fun equals(source: String, pattern: String) : Boolean =
if (source.length != pattern.length) false else source.hash() == pattern.hash()
/**
*
* computes the hash of a string according to the formula:
*
* hash(abc) = a.code * primeCoefficient⁰ + b.code * primeCoefficient¹ + c.code * primeCoefficient²
*
*/
private fun String.hash() : Int {
var result = 0
var factor = 1
forEach { symbol ->
result += symbol.code * factor
factor *= PRIME_COEFFICIENT
}
// the hash can exceed the maximum Int value, so we limit it
return result.mod(Int.MAX_VALUE)
}
companion object {
/**
*
* I chose the nearest prime number for the size of the alphabet
*
* my alphabet is [a-z] [A-Z] ! , .
*
* size of the alphabet = 26 + 26 + 3 = 55 (is not prime)
*
*/
private const val PRIME_COEFFICIENT = 53
}
}