Longest Subsequence Repeated k Times LeetCode solution in Java
1class Solution {2char[] A;34public String longestSubsequenceRepeatedK(String s, int k) {5A = s.toCharArray();6Queue<String> queue = new ArrayDeque<>();7queue.offer("");8String ans = "";9int[] count = new int[26];10BitSet bit = new BitSet();11for (char ch : A)12if (++count[ch - 'a'] >= k) {13bit.set(ch - 'a');14}15while (!queue.isEmpty()) {16String sb = queue.poll();17for (int i = bit.nextSetBit(0); i >= 0; i = bit.nextSetBit(i + 1)) {18String res = sb + (char) (i + 'a');19if (check(k, res)) {20ans = res;21queue.offer(res);22}23}24}25return ans;26}2728private boolean check(int k, String s) {29int cnt = 0;30for (char ch : A) {31if (s.charAt(cnt % s.length()) == ch && ++cnt >= k * s.length()) {32return true;33}34}35return false;36}37}