Longest Subsequence Repeated k Times LeetCode solution in C++

1
class Solution {
2
public:
3
int Alpha = 26;
4
bool find(string &s, string &p, int k) {
5
int j = 0;
6
int n = s.size();
7
int count = 0;
8
for (int i = 0; i < n; i++) {
9
if (s[i] == p[j]) {
10
j++;
11
if (j == p.size()) {
12
count++;
13
j = 0;
14
}
15
if (count == k) {
16
return true;
17
}
18
}
19
}
20
return false;
21
}
22
string longestSubsequenceRepeatedK(string s, int k) {
23
queue<string> q;
24
q.push("");
25
string ans = "";
26
while (q.size()) {
27
auto temp = q.front();
28
q.pop();
29
for (int i = 0; i < Alpha; i++) {
30
string curr = temp + char('a' + i);
31
if (find(s, curr, k)) {
32
ans = curr;
33
q.push(curr);
34
}
35
}
36
}
37
return ans;
38
}
39
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0