Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 4c2916c

Browse files
authoredJul 8, 2021
Update 1554-strings-differ-by-one-character.js
1 parent 434170a commit 4c2916c

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed
 

‎1554-strings-differ-by-one-character.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,27 @@
2424
*/
2525
const differByOne = function (dict) {
2626
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),
2929
ord = (c) => c.charCodeAt(0),
30-
MOD = 1e13, seen = new Set()
30+
MOD = 1e13, seen = new Set(),
31+
zPlusOne = 'z'.charCodeAt(0)
3132
// 1. generate each i-th rolling hash
3233
for (let i = 0; i < M; ++i) {
3334
let base = 1
3435
for (let j = 0; j < N; ++j) {
3536
hash[i] = (hash[i] + base * ord(dict[i][j])) % MOD
36-
base = (123 * base) % MOD
37+
base = (zPlusOne * base) % MOD
3738
}
3839
}
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
4041
for (let i = 0; i < M; ++i) {
4142
let base = 1
4243
for (let j = 0; j < N; ++j) {
4344
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
4546
seen.add(diff)
46-
base = (123 * base) % MOD
47+
base = (zPlusOne * base) % MOD
4748
}
4849
}
4950
return false

0 commit comments

Comments
 (0)
Please sign in to comment.