-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
minimum-length-of-anagram-concatenation.cpp
44 lines (41 loc) · 1.21 KB
/
minimum-length-of-anagram-concatenation.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
40
41
42
43
44
// Time: O(sqrt(n) * n + (26 * sum(n/i for i in range(1, n+1) if n%i == 0))) < O(sqrt(n) * n + 26 * sum(n/i for i in range(1, n+1)) = O(sqrt(n) * n + 26 * nlogn)
// Space: O(26)
// number theory, freq table
class Solution {
public:
int minAnagramLength(string s) {
const auto& check = [&](int l) {
const auto& count = [&](int i) {
vector<int> cnt(26);
for (int j = i; j < i + l; ++j) {
++cnt[s[j] - 'a'];
}
return cnt;
};
const auto& cnt = count(0);
for (int i = l; i < size(s); i += l) {
if (count(i) != cnt) {
return false;
}
}
return true;
};
int result = size(s);
for (int i = 1; i * i <= size(s); ++i) {
if (size(s) % i) {
continue;
}
if (check(i)) {
result = min(result, i);
}
const int j = size(s) / i;
if (j == i) {
continue;
}
if (check(j)) {
result = min(result, j);
}
}
return result;
}
};