Skip to content

feat: add solutions to lc problem: No.2014 #4526

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 26, 2025
Merged
Show file tree
Hide file tree
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
226 changes: 222 additions & 4 deletions solution/2000-2099/2014.Longest Subsequence Repeated k Times/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,32 +80,250 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:BFS

我们可以先统计字符串中每个字符出现的次数,然后将出现次数大于等于 $k$ 的字符按从小到大的顺序存入一个列表 $\textit{cs}$ 中。接下来,我们可以使用 BFS 来枚举所有可能的子序列。

我们定义一个队列 $\textit{q}$,初始时将空字符串放入队列中。然后,我们从队列中取出一个字符串 $\textit{cur}$,并尝试将每个字符 $c \in \textit{cs}$ 添加到 $\textit{cur}$ 的末尾,形成一个新的字符串 $\textit{nxt}$。如果 $\textit{nxt}$ 是一个重复 $k$ 次的子序列,我们就将其加入到答案中,并将 $\textit{nxt}$ 放入队列中继续处理。

我们需要一个辅助函数 $\textit{check(t, k)}$ 来判断字符串 $\textit{t}$ 是否是字符串 $s$ 的一个重复 $k$ 次的子序列。具体地,我们可以使用两个指针来遍历字符串 $s$ 和 $\textit{t}$,如果在遍历过程中能够找到 $\textit{t}$ 的所有字符,并且能够重复 $k$ 次,那么就返回 $\textit{true}$,否则返回 $\textit{false}$。

<!-- tabs:start -->

#### Python3

```python

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
```

#### Java

```java

class Solution {
private char[] s;

public String longestSubsequenceRepeatedK(String s, int k) {
this.s = s.toCharArray();
int[] cnt = new int[26];
for (char c : this.s) {
cnt[c - 'a']++;
}

List<Character> cs = new ArrayList<>();
for (char c = 'a'; c <= 'z'; ++c) {
if (cnt[c - 'a'] >= k) {
cs.add(c);
}
}
Deque<String> q = new ArrayDeque<>();
q.offer("");
String ans = "";
while (!q.isEmpty()) {
String cur = q.poll();
for (char c : cs) {
String nxt = cur + c;
if (check(nxt, k)) {
ans = nxt;
q.offer(nxt);
}
}
}
return ans;
}

private boolean check(String t, int k) {
int i = 0;
for (char c : s) {
if (c == t.charAt(i)) {
i++;
if (i == t.length()) {
if (--k == 0) {
return true;
}
i = 0;
}
}
}
return false;
}
}
```

#### C++

```cpp

class Solution {
public:
string longestSubsequenceRepeatedK(string s, int k) {
auto check = [&](const string& t, int k) -> bool {
int i = 0;
for (char c : s) {
if (c == t[i]) {
i++;
if (i == t.size()) {
if (--k == 0) {
return true;
}
i = 0;
}
}
}
return false;
};
int cnt[26] = {};
for (char c : s) {
cnt[c - 'a']++;
}

vector<char> cs;
for (char c = 'a'; c <= 'z'; ++c) {
if (cnt[c - 'a'] >= k) {
cs.push_back(c);
}
}

queue<string> q;
q.push("");
string ans;
while (!q.empty()) {
string cur = q.front();
q.pop();
for (char c : cs) {
string nxt = cur + c;
if (check(nxt, k)) {
ans = nxt;
q.push(nxt);
}
}
}
return ans;
}
};
```

#### Go

```go
func longestSubsequenceRepeatedK(s string, k int) string {
check := func(t string, k int) bool {
i := 0
for _, c := range s {
if byte(c) == t[i] {
i++
if i == len(t) {
k--
if k == 0 {
return true
}
i = 0
}
}
}
return false
}

cnt := [26]int{}
for i := 0; i < len(s); i++ {
cnt[s[i]-'a']++
}

cs := []byte{}
for c := byte('a'); c <= 'z'; c++ {
if cnt[c-'a'] >= k {
cs = append(cs, c)
}
}

q := []string{""}
ans := ""
for len(q) > 0 {
cur := q[0]
q = q[1:]
for _, c := range cs {
nxt := cur + string(c)
if check(nxt, k) {
ans = nxt
q = append(q, nxt)
}
}
}
return ans
}
```

#### TypeScript

```ts
function longestSubsequenceRepeatedK(s: string, k: number): string {
const check = (t: string, k: number): boolean => {
let i = 0;
for (const c of s) {
if (c === t[i]) {
i++;
if (i === t.length) {
k--;
if (k === 0) {
return true;
}
i = 0;
}
}
}
return false;
};

const cnt = new Array(26).fill(0);
for (const c of s) {
cnt[c.charCodeAt(0) - 97]++;
}

const cs: string[] = [];
for (let i = 0; i < 26; ++i) {
if (cnt[i] >= k) {
cs.push(String.fromCharCode(97 + i));
}
}

const q: string[] = [''];
let ans = '';
while (q.length > 0) {
const cur = q.shift()!;
for (const c of cs) {
const nxt = cur + c;
if (check(nxt, k)) {
ans = nxt;
q.push(nxt);
}
}
}

return ans;
}
```

<!-- tabs:end -->
Expand Down
Loading