LeetCode 2014. Longest Subsequence Repeated k Times Solution in Java, C++, Python & More | Explanation + Code

CoderIndeed
0
2014. Longest Subsequence Repeated k Times

Description

You are given a string s of length n, and an integer k. You are tasked to find the longest subsequence repeated k times in string s.

A subsequence is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.

A subsequence seq is repeated k times in the string s if seq * k is a subsequence of s, where seq * k represents a string constructed by concatenating seq k times.

  • For example, "bba" is repeated 2 times in the string "bababcba", because the string "bbabba", constructed by concatenating "bba" 2 times, is a subsequence of the string "bababcba".

Return the longest subsequence repeated k times in string s. If multiple such subsequences are found, return the lexicographically largest one. If there is no such subsequence, return an empty string.

 

Example 1:

example 1
Input: s = "letsleetcode", k = 2
Output: "let"
Explanation: There are two longest subsequences repeated 2 times: "let" and "ete".
"let" is the lexicographically largest one.

Example 2:

Input: s = "bb", k = 2
Output: "b"
Explanation: The longest subsequence repeated 2 times is "b".

Example 3:

Input: s = "ab", k = 2
Output: ""
Explanation: There is no subsequence repeated 2 times. Empty string is returned.

 

Constraints:

  • n == s.length
  • 2 <= k <= 2000
  • 2 <= n < min(2001, k * 8)
  • s consists of lowercase English letters.

Solutions

Solution 1: BFS

We can first count the occurrences of each character in the string, and then store the characters that appear at least k times in a list cs in ascending order. Next, we can use BFS to enumerate all possible subsequences.

We define a queue q, initially putting the empty string into the queue. Then, we take out a string cur from the queue and try to append each character c ∈ cs to the end of cur to form a new string nxt. If nxt is a subsequence that can be repeated k times, we add it to the answer and put nxt back into the queue for further processing.

We need an auxiliary function check(t, k) to determine whether the string t is a repeated k times subsequence of string s. Specifically, we can use two pointers to traverse s and t. If we can find all characters of t in s and repeat this process k times, then return true; otherwise, return false.

PythonJavaC++GoTypeScript
class Solution: def longestSubsequenceRepeatedK(self, s: str, k: int) -> str: def check(t: str, k: int) -> bool: i = 0 for c in s: if c == t[i]: i += 1 if i == len(t): k -= 1 if k == 0: return True i = 0 return False cnt = Counter(s) cs = [c for c in ascii_lowercase if cnt[c] >= k] q = deque([""]) ans = "" while q: cur = q.popleft() for c in cs: nxt = cur + c if check(nxt, k): ans = nxt q.append(nxt) return ans(code-box)

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !