Skip to content

Commit 5375fee

Browse files
authored
Update 2014-longest-subsequence-repeated-k-times.js
1 parent 7b38fc5 commit 5375fee

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

2014-longest-subsequence-repeated-k-times.js

+50
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,53 @@
1+
/**
2+
* @param {string} s
3+
* @param {number} k
4+
* @return {string}
5+
*/
6+
const longestSubsequenceRepeatedK = function(s, k) {
7+
const n = s.length, a = 'a'.charCodeAt(0)
8+
9+
let res = ''
10+
const q = ['']
11+
12+
while(q.length) {
13+
const size = q.length
14+
for(let i = 0; i < size; i++) {
15+
const cur = q.shift()
16+
for(let j = 0; j < 26; j++) {
17+
const next = cur + String.fromCharCode(a + j)
18+
if(isSub(s, next, k)) {
19+
res = next
20+
q.push(next)
21+
}
22+
}
23+
24+
}
25+
}
26+
27+
return res
28+
29+
30+
function isSub(s, p, k) {
31+
let repeated = 0
32+
for(let i = 0, j = 0, n = s.length, m = p.length; i < n; i++) {
33+
if(s[i] === p[j]) {
34+
j++
35+
if(j === m) {
36+
repeated++
37+
j = 0
38+
if(repeated === k) {
39+
return true
40+
}
41+
}
42+
}
43+
}
44+
45+
return false
46+
}
47+
};
48+
49+
// another
50+
151
/**
252
* @param {string} s
353
* @param {number} k

0 commit comments

Comments
 (0)