-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsolution.cpp
41 lines (39 loc) · 1.13 KB
/
solution.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
/**
* 57 / 57 test cases passed.
* Runtime: 8 ms
* Memory Usage: 6.8 MB
*/
class Solution {
public:
int repeatedStringMatch(string a, string b) {
uint32_t a_bmp = 0, b_bmp = 0;
for (auto &c : a) a_bmp |= 1 << (c - 'a');
for (auto &c : b) b_bmp |= 1 << (c - 'a');
// set(B) is subset of set(A)
if (a_bmp & b_bmp != b_bmp) return -1;
int times = (b.size() - 1) / a.size() + 1;
string pattern = "";
for (int i = 0; i < times; ++ i) pattern += a;
if (pattern.find(b) != pattern.npos) return times;
pattern += a;
if (pattern.find(b) != pattern.npos) return times + 1;
return -1;
}
};
/**
* 57 / 57 test cases passed.
* Runtime: 8 ms
* Memory Usage: 6.7 MB
*/
class Solution2 {
public:
int repeatedStringMatch(string a, string b) {
int times = (b.size() - 1) / a.size() + 1;
string pattern = "";
for (int i = 0; i < times; ++ i) pattern += a;
if (pattern.find(b) != pattern.npos) return times;
pattern += a;
if (pattern.find(b) != pattern.npos) return times + 1;
return -1;
}
};