Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions 2014. Longest Subsequence Repeated k Times
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
class Solution {
public:
bool isKRepeatedSubsequence(const string& s, const string& pattern, int k) {
int pos = 0, matched = 0;
int m = pattern.size();

for (char ch : s) {
if (ch == pattern[pos]) {
pos++;
if (pos == m) {
pos = 0;
matched++;
if (matched == k) return true;
}
}
}
return false;
}

string longestSubsequenceRepeatedK(string s, int k) {
vector<int> freq(26, 0);
for (char ch : s) freq[ch - 'a']++;

vector<char> candidates;
for (int i = 25; i >= 0; --i) {
if (freq[i] >= k) candidates.push_back('a' + i);
}

queue<string> q;
string ans = "";

for (char ch : candidates) {
q.push(string(1, ch));
}

while (!q.empty()) {
string curr = q.front(); q.pop();

if (curr.size() > ans.size() || (curr.size() == ans.size() && curr > ans)) {
if (isKRepeatedSubsequence(s, curr, k)) ans = curr;
}

if (curr.size() == 7) continue;

for (char ch : candidates) {
string next = curr + ch;
if (isKRepeatedSubsequence(s, next, k)) q.push(next);
}
}

return ans;
}
};
Loading