Skip to content

Commit d4e7d91

Browse files
committed
Runtime: 1501 ms (Top 55.56%) | Memory: 58.7 MB (Top 11.11%)
1 parent ce0ad80 commit d4e7d91

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

scripts/algorithms/L/Longest Subsequence Repeated k Times/Longest Subsequence Repeated k Times.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Runtime: 1501 ms (Top 55.56%) | Memory: 58.7 MB (Top 11.11%)
12
// Idea comes from:
23
// https://leetcode.com/problems/longest-subsequence-repeated-k-times/discuss/1471930/Python-Answer-is-not-so-long-explained
34

@@ -7,35 +8,35 @@ var longestSubsequenceRepeatedK = function(s, k) {
78
if (!freq[c]) freq[c] = 0;
89
freq[c]++;
910
}
10-
11+
1112
// Find hot string
1213
let hot = "";
1314
for (const [c, cnt] of Object.entries(freq)) {
1415
const repeat = Math.floor(cnt / k);
1516
hot += c.repeat(repeat);
1617
}
17-
18+
1819
// Find all subset and permutation of hot string
1920
let targets = new Set();
2021
const subsets = getSubset(hot);
2122
for (const subset of subsets) {
2223
const permutations = getPermutation(subset);
2324
for (const per of permutations) targets.add(per);
2425
}
25-
26+
2627
// Sort targets by length and lexico
2728
targets = [...targets].sort((a, b) => {
2829
const delta = b.length - a.length;
2930
if (delta) return delta;
3031
return b.localeCompare(a);
3132
});
32-
33+
3334
// Filter s and check subsequence
3435
s = [...s].filter(c => hot.includes(c)).join("");
3536
for (const tar of targets) {
3637
if (isSubsequence(s, tar.repeat(k))) return tar;
3738
}
38-
39+
3940
return "";
4041
};
4142

@@ -71,4 +72,4 @@ function isSubsequence(s, t) {
7172
}
7273
}
7374
return false;
74-
}
75+
}

0 commit comments

Comments
 (0)