File tree 1 file changed +8
-7
lines changed
1 file changed +8
-7
lines changed Original file line number Diff line number Diff line change 24
24
*/
25
25
const differByOne = function ( dict ) {
26
26
const M = dict . length ,
27
- N = dict [ 0 ] . length
28
- const hash = Array ( M ) . fill ( 0 ) ,
27
+ N = dict [ 0 ] . length ,
28
+ hash = Array ( M ) . fill ( 0 ) ,
29
29
ord = ( c ) => c . charCodeAt ( 0 ) ,
30
- MOD = 1e13 , seen = new Set ( )
30
+ MOD = 1e13 , seen = new Set ( ) ,
31
+ zPlusOne = 'z' . charCodeAt ( 0 )
31
32
// 1. generate each i-th rolling hash
32
33
for ( let i = 0 ; i < M ; ++ i ) {
33
34
let base = 1
34
35
for ( let j = 0 ; j < N ; ++ j ) {
35
36
hash [ i ] = ( hash [ i ] + base * ord ( dict [ i ] [ j ] ) ) % MOD
36
- base = ( 123 * base ) % MOD
37
+ base = ( zPlusOne * base ) % MOD
37
38
}
38
39
}
39
- // 2. remove each j-th char from each i-th rolling hash to 🔍 find a diff collision 💥
40
+ // 2. remove each j-th char from each i-th rolling hash to find a diff collision
40
41
for ( let i = 0 ; i < M ; ++ i ) {
41
42
let base = 1
42
43
for ( let j = 0 ; j < N ; ++ j ) {
43
44
const diff = ( hash [ i ] - base * ord ( dict [ i ] [ j ] ) ) % MOD
44
- if ( seen . has ( diff ) ) return true // 🎯 found a diff collision 💥
45
+ if ( seen . has ( diff ) ) return true
45
46
seen . add ( diff )
46
- base = ( 123 * base ) % MOD
47
+ base = ( zPlusOne * base ) % MOD
47
48
}
48
49
}
49
50
return false
You can’t perform that action at this time.
0 commit comments