Skip to content

Commit 90877bf

Browse files
authored
feat: add solutions to lc problems: No.2572,2573 (#2379)
* No.2572.Count the Number of Square-Free Subsets * No.2573.Find the String with LCP
1 parent b84fd10 commit 90877bf

File tree

6 files changed

+234
-0
lines changed

6 files changed

+234
-0
lines changed

solution/2500-2599/2572.Count the Number of Square-Free Subsets/README.md

+39
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,45 @@ func squareFreeSubsets(nums []int) (ans int) {
213213
}
214214
```
215215

216+
```ts
217+
function squareFreeSubsets(nums: number[]): number {
218+
const primes: number[] = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29];
219+
const cnt: number[] = Array(31).fill(0);
220+
for (const x of nums) {
221+
++cnt[x];
222+
}
223+
const mod: number = Math.pow(10, 9) + 7;
224+
const n: number = primes.length;
225+
const f: number[] = Array(1 << n).fill(0);
226+
f[0] = 1;
227+
for (let i = 0; i < cnt[1]; ++i) {
228+
f[0] = (f[0] * 2) % mod;
229+
}
230+
for (let x = 2; x < 31; ++x) {
231+
if (cnt[x] === 0 || x % 4 === 0 || x % 9 === 0 || x % 25 === 0) {
232+
continue;
233+
}
234+
let mask: number = 0;
235+
for (let i = 0; i < n; ++i) {
236+
if (x % primes[i] === 0) {
237+
mask |= 1 << i;
238+
}
239+
}
240+
for (let state = (1 << n) - 1; state > 0; --state) {
241+
if ((state & mask) === mask) {
242+
f[state] = (f[state] + cnt[x] * f[state ^ mask]) % mod;
243+
}
244+
}
245+
}
246+
let ans: number = 0;
247+
for (let i = 0; i < 1 << n; ++i) {
248+
ans = (ans + f[i]) % mod;
249+
}
250+
ans -= 1;
251+
return ans >= 0 ? ans : ans + mod;
252+
}
253+
```
254+
216255
<!-- tabs:end -->
217256

218257
<!-- end -->

solution/2500-2599/2572.Count the Number of Square-Free Subsets/README_EN.md

+39
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,45 @@ func squareFreeSubsets(nums []int) (ans int) {
210210
}
211211
```
212212

213+
```ts
214+
function squareFreeSubsets(nums: number[]): number {
215+
const primes: number[] = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29];
216+
const cnt: number[] = Array(31).fill(0);
217+
for (const x of nums) {
218+
++cnt[x];
219+
}
220+
const mod: number = Math.pow(10, 9) + 7;
221+
const n: number = primes.length;
222+
const f: number[] = Array(1 << n).fill(0);
223+
f[0] = 1;
224+
for (let i = 0; i < cnt[1]; ++i) {
225+
f[0] = (f[0] * 2) % mod;
226+
}
227+
for (let x = 2; x < 31; ++x) {
228+
if (cnt[x] === 0 || x % 4 === 0 || x % 9 === 0 || x % 25 === 0) {
229+
continue;
230+
}
231+
let mask: number = 0;
232+
for (let i = 0; i < n; ++i) {
233+
if (x % primes[i] === 0) {
234+
mask |= 1 << i;
235+
}
236+
}
237+
for (let state = (1 << n) - 1; state > 0; --state) {
238+
if ((state & mask) === mask) {
239+
f[state] = (f[state] + cnt[x] * f[state ^ mask]) % mod;
240+
}
241+
}
242+
}
243+
let ans: number = 0;
244+
for (let i = 0; i < 1 << n; ++i) {
245+
ans = (ans + f[i]) % mod;
246+
}
247+
ans -= 1;
248+
return ans >= 0 ? ans : ans + mod;
249+
}
250+
```
251+
213252
<!-- tabs:end -->
214253

215254
<!-- end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
function squareFreeSubsets(nums: number[]): number {
2+
const primes: number[] = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29];
3+
const cnt: number[] = Array(31).fill(0);
4+
for (const x of nums) {
5+
++cnt[x];
6+
}
7+
const mod: number = Math.pow(10, 9) + 7;
8+
const n: number = primes.length;
9+
const f: number[] = Array(1 << n).fill(0);
10+
f[0] = 1;
11+
for (let i = 0; i < cnt[1]; ++i) {
12+
f[0] = (f[0] * 2) % mod;
13+
}
14+
for (let x = 2; x < 31; ++x) {
15+
if (cnt[x] === 0 || x % 4 === 0 || x % 9 === 0 || x % 25 === 0) {
16+
continue;
17+
}
18+
let mask: number = 0;
19+
for (let i = 0; i < n; ++i) {
20+
if (x % primes[i] === 0) {
21+
mask |= 1 << i;
22+
}
23+
}
24+
for (let state = (1 << n) - 1; state > 0; --state) {
25+
if ((state & mask) === mask) {
26+
f[state] = (f[state] + cnt[x] * f[state ^ mask]) % mod;
27+
}
28+
}
29+
}
30+
let ans: number = 0;
31+
for (let i = 0; i < 1 << n; ++i) {
32+
ans = (ans + f[i]) % mod;
33+
}
34+
ans -= 1;
35+
return ans >= 0 ? ans : ans + mod;
36+
}

solution/2500-2599/2573.Find the String with LCP/README.md

+41
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,47 @@ func findTheString(lcp [][]int) string {
228228
}
229229
```
230230

231+
```ts
232+
function findTheString(lcp: number[][]): string {
233+
let i: number = 0;
234+
const n: number = lcp.length;
235+
let s: string = '\0'.repeat(n);
236+
for (let ascii = 97; ascii < 123; ++ascii) {
237+
const c: string = String.fromCharCode(ascii);
238+
while (i < n && s[i] !== '\0') {
239+
++i;
240+
}
241+
if (i === n) {
242+
break;
243+
}
244+
for (let j = i; j < n; ++j) {
245+
if (lcp[i][j]) {
246+
s = s.substring(0, j) + c + s.substring(j + 1);
247+
}
248+
}
249+
}
250+
if (s.indexOf('\0') !== -1) {
251+
return '';
252+
}
253+
for (i = n - 1; ~i; --i) {
254+
for (let j = n - 1; ~j; --j) {
255+
if (s[i] === s[j]) {
256+
if (i === n - 1 || j === n - 1) {
257+
if (lcp[i][j] !== 1) {
258+
return '';
259+
}
260+
} else if (lcp[i][j] !== lcp[i + 1][j + 1] + 1) {
261+
return '';
262+
}
263+
} else if (lcp[i][j]) {
264+
return '';
265+
}
266+
}
267+
}
268+
return s;
269+
}
270+
```
271+
231272
<!-- tabs:end -->
232273

233274
<!-- end -->

solution/2500-2599/2573.Find the String with LCP/README_EN.md

+41
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,47 @@ func findTheString(lcp [][]int) string {
224224
}
225225
```
226226

227+
```ts
228+
function findTheString(lcp: number[][]): string {
229+
let i: number = 0;
230+
const n: number = lcp.length;
231+
let s: string = '\0'.repeat(n);
232+
for (let ascii = 97; ascii < 123; ++ascii) {
233+
const c: string = String.fromCharCode(ascii);
234+
while (i < n && s[i] !== '\0') {
235+
++i;
236+
}
237+
if (i === n) {
238+
break;
239+
}
240+
for (let j = i; j < n; ++j) {
241+
if (lcp[i][j]) {
242+
s = s.substring(0, j) + c + s.substring(j + 1);
243+
}
244+
}
245+
}
246+
if (s.indexOf('\0') !== -1) {
247+
return '';
248+
}
249+
for (i = n - 1; ~i; --i) {
250+
for (let j = n - 1; ~j; --j) {
251+
if (s[i] === s[j]) {
252+
if (i === n - 1 || j === n - 1) {
253+
if (lcp[i][j] !== 1) {
254+
return '';
255+
}
256+
} else if (lcp[i][j] !== lcp[i + 1][j + 1] + 1) {
257+
return '';
258+
}
259+
} else if (lcp[i][j]) {
260+
return '';
261+
}
262+
}
263+
}
264+
return s;
265+
}
266+
```
267+
227268
<!-- tabs:end -->
228269

229270
<!-- end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
function findTheString(lcp: number[][]): string {
2+
let i: number = 0;
3+
const n: number = lcp.length;
4+
let s: string = '\0'.repeat(n);
5+
for (let ascii = 97; ascii < 123; ++ascii) {
6+
const c: string = String.fromCharCode(ascii);
7+
while (i < n && s[i] !== '\0') {
8+
++i;
9+
}
10+
if (i === n) {
11+
break;
12+
}
13+
for (let j = i; j < n; ++j) {
14+
if (lcp[i][j]) {
15+
s = s.substring(0, j) + c + s.substring(j + 1);
16+
}
17+
}
18+
}
19+
if (s.indexOf('\0') !== -1) {
20+
return '';
21+
}
22+
for (i = n - 1; ~i; --i) {
23+
for (let j = n - 1; ~j; --j) {
24+
if (s[i] === s[j]) {
25+
if (i === n - 1 || j === n - 1) {
26+
if (lcp[i][j] !== 1) {
27+
return '';
28+
}
29+
} else if (lcp[i][j] !== lcp[i + 1][j + 1] + 1) {
30+
return '';
31+
}
32+
} else if (lcp[i][j]) {
33+
return '';
34+
}
35+
}
36+
}
37+
return s;
38+
}

0 commit comments

Comments
 (0)