-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution2842.java
More file actions
40 lines (37 loc) · 1.06 KB
/
Copy pathSolution2842.java
File metadata and controls
40 lines (37 loc) · 1.06 KB
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
class Solution2842 {
private long res = 0;
private int mod = 1000000007;
private int[] nums = new int[26];
private long sum = 0; // 美丽值
private int k;
public int countKSubsequencesWithMaxBeauty(String s, int k) {
this.k = k;
for (int i = 0; i < s.length(); i++) {
nums[s.charAt(i) - 'a']++;
}
dfs(0, 0, 0, 1);
return (int) res;
}
public void dfs(int index, int len, long value, long mul) {
/*
index : 当前下标
len : 当前子序列长度
n : 美丽值
mul : 数量
*/
// 递归出口
if (len == k) {
if (value > sum) {
sum = value; // 更新最大美丽值
res = mul % mod;
} else if (value == sum) {
res = (res + mul) % mod;
}
return;
}
for (int i = index; i < nums.length; i++) {
if (nums[i] == 0) continue;
dfs(i + 1, len + 1, value + nums[i], (mul * nums[i]) % mod);
}
}
}