forked from PRAteek-singHWY/hackoctoberfest2024
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongest-Subsequence-Repeated-k-Times.cpp
More file actions
50 lines (47 loc) Β· 1.23 KB
/
Copy pathLongest-Subsequence-Repeated-k-Times.cpp
File metadata and controls
50 lines (47 loc) Β· 1.23 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
41
42
43
44
45
46
47
48
49
50
//bfs is new concept i have watched a video to solve it
class Solution {
int countSubsequences(const string& s,const string& next){
int i=0;
int j=0;
int m = next.size();
int subsequence_count = 0;
while(i<s.size()){
if(s[i]==next[j]){
j++;
if(j==m){
j=0;
subsequence_count++;
}
}
i++;
}
return subsequence_count;
}
public:
string longestSubsequenceRepeatedK(string s, int k) {
int n=s.size();
vector<int> freq(26);
for(int i=0;i<n;++i)
freq[s[i]-'a']++;
string curr = "";
queue<string> q;
q.push(curr);
string res;
while(!q.empty()){
curr = q.front();
q.pop();
string next = curr;
for(char c='a';c<='z';++c){
if(freq[c-'a']<k)
continue;
next.push_back(c);
if(countSubsequences(s,next)>=k){
res = next;
q.push(next);
}
next.pop_back();
}
}
return res;
}
};