|
| 1 | +import java.util.*; |
| 2 | + |
| 3 | +public class Solution { |
| 4 | + public String longestSubsequenceRepeatedK(String s, int k) { |
| 5 | + // Step 1: Frequency counter for each character in the string |
| 6 | + Map<Character, Integer> freq = new HashMap<>(); |
| 7 | + for (char c : s.toCharArray()) { |
| 8 | + freq.put(c, freq.getOrDefault(c, 0) + 1); |
| 9 | + } |
| 10 | + |
| 11 | + // Step 2: Collect all characters with at least k frequency |
| 12 | + List<Character> candidates = new ArrayList<>(); |
| 13 | + for (Map.Entry<Character, Integer> entry : freq.entrySet()) { |
| 14 | + if (entry.getValue() >= k) { |
| 15 | + candidates.add(entry.getKey()); |
| 16 | + } |
| 17 | + } |
| 18 | + |
| 19 | + // Step 4: Breadth-First Search (BFS) to generate subsequences |
| 20 | + Queue<String> queue = new LinkedList<>(); |
| 21 | + queue.offer(""); // Start with an empty sequence |
| 22 | + String longest = ""; |
| 23 | + |
| 24 | + while (!queue.isEmpty()) { |
| 25 | + String curr = queue.poll(); |
| 26 | + |
| 27 | + // Try appending each candidate character to the current subsequence |
| 28 | + for (char ch : candidates) { |
| 29 | + String newSubseq = curr + ch; |
| 30 | + |
| 31 | + // Check if the new subsequence is repeatable k times |
| 32 | + if (canBeRepeatedKTimes(newSubseq, s, k)) { |
| 33 | + queue.offer(newSubseq); |
| 34 | + |
| 35 | + // Update longest if newSubseq is longer or lexicographically larger |
| 36 | + if (newSubseq.length() > longest.length() || |
| 37 | + (newSubseq.length() == longest.length() && newSubseq.compareTo(longest) > 0)) { |
| 38 | + longest = newSubseq; |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + return longest; |
| 45 | + } |
| 46 | + |
| 47 | + // Helper function to check if a subsequence can be repeated k times |
| 48 | + private boolean canBeRepeatedKTimes(String subseq, String s, int k) { |
| 49 | + int i = 0, count = 0; |
| 50 | + for (char ch : s.toCharArray()) { |
| 51 | + if (ch == subseq.charAt(i)) { |
| 52 | + i++; |
| 53 | + if (i == subseq.length()) { |
| 54 | + i = 0; |
| 55 | + count++; |
| 56 | + if (count == k) { |
| 57 | + return true; |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + return false; |
| 63 | + } |
| 64 | + |
| 65 | + public static void main(String[] args) { |
| 66 | + Solution sol = new Solution(); |
| 67 | + |
| 68 | + // Test case 1 |
| 69 | + String s1 = "letsleetcode"; |
| 70 | + int k1 = 2; |
| 71 | + System.out.println(sol.longestSubsequenceRepeatedK(s1, k1)); // Output: "let" |
| 72 | + |
| 73 | + // Test case 2 |
| 74 | + String s2 = "bb"; |
| 75 | + int k2 = 2; |
| 76 | + System.out.println(sol.longestSubsequenceRepeatedK(s2, k2)); // Output: "b" |
| 77 | + |
| 78 | + // Test case 3 |
| 79 | + String s3 = "ab"; |
| 80 | + int k3 = 2; |
| 81 | + System.out.println(sol.longestSubsequenceRepeatedK(s3, k3)); // Output: "" |
| 82 | + |
| 83 | + // Test case 4 |
| 84 | + String s4 = "bbabbabbbbabaababab"; |
| 85 | + int k4 = 3; |
| 86 | + System.out.println(sol.longestSubsequenceRepeatedK(s4, k4)); // Output: "bbbb" |
| 87 | + } |
| 88 | +} |
0 commit comments