-
Notifications
You must be signed in to change notification settings - Fork 3
/
1554.cpp
39 lines (38 loc) · 1.31 KB
/
1554.cpp
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
class Solution {
public:
bool check(string& s1, string& s2, int j, int m) {
for (int i = 0; i < m; ++i) {
if (i != j && s1[i] != s2[i]) return false;
if (i == j && s1[i] == s2[i]) return false;
}
return true;
}
bool differByOne(vector<string>& dict) {
int n = dict.size();
int m = dict[0].size();
long long mod = 1e9 + 7;
vector<long long> hash(n, 0);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
hash[i] = ((hash[i] * 26) + (dict[i][j] - 'a')) % mod;
}
}
long long multiply = 1;
for (int j = m - 1; j >= 0; --j) {
unordered_map<long long, vector<int>> mp;
for (int i = 0; i < n; ++i) {
long long adjustHash = (hash[i] - dict[i][j] * multiply + mod) % mod;
if (mp.find(adjustHash) != mp.end()) {
for (auto collision : mp[adjustHash]) {
// check if there are only #j different
if (check(dict[i], dict[collision], j, m)) return true;
}
}
mp[adjustHash].push_back(i);
}
multiply *= 26;
multiply %= mod;
}
return false;
}
};