deno.land / x / masx200_leetcode_test@10.6.5 / k-similar-strings / index.ts

نووسراو ببینە
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
export default function kSimilarity(s1: string, s2: string): number { let str1 = ""; let str2 = ""; for (let i = 0; i < s1.length; i++) { if (s1[i] !== s2[i]) { str1 += s1[i]; str2 += s2[i]; } } if (str1.length === 0) { return 0; } let ans = str1.length - 1;
function dfs( pos: number, cost: number, len: number, str1: string | string[], str2: string, ) { if (cost > ans) { return; } while (pos < str1.length && str1[pos] === str2[pos]) { pos++; } if (pos === str1.length) { ans = Math.min(ans, cost); return; } /* 当前状态的交换次数下限大于等于当前的最小交换次数 */ if (cost + minSwap(str1, str2, pos) >= ans) { return; } for (let i = pos + 1; i < str1.length; i++) { if (str1[i] === str2[pos]) { const str1Next = swap(str1, i, pos); dfs(pos + 1, cost + 1, len, str1Next, str2); } } }
dfs(0, 0, str1.length, str1, str2); return ans;}function minSwap(s1: string | string[], s2: string | string[], pos: number) { let tot = 0; for (let i = pos; i < s1.length; i++) { tot += s1[i] !== s2[i] ? 1 : 0; } return Math.floor((tot + 1) / 2);}
function swap(cur: string | string[], i: number, j: number) { const arr = [...cur]; const c = arr[i]; arr[i] = arr[j]; arr[j] = c; return arr;}
masx200_leetcode_test

Version Info

Tagged at
a year ago