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