Skip to content

Commit e4f4a3e

Browse files
committed
Add 395
1 parent 247cfcc commit e4f4a3e

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

Readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
| 389 | Find the Difference | Medium | O(N) | O(1) | Hash Table, Bit Manipulation | | |
9090
| 387 | First Unique Character in a String | Easy | O(N) | O(N) | | | |
9191
| 394 | Decode String | Medium | O(maxK*N) | O(N) | Stack, DFS | | |
92+
| 395 | Longest Substring with At Least K Repeating Characters | Medium | O(NlogN) | O(N) | Divide and Conquer, Recursion, Sliding Window | | |
9293
| 399 | Evaluate Division | Medium | O(N*M) | O(N) | Union Find, Graph | | |
9394
| 402 | Remove K Digits | Medium | O(N) | O(N) | Greedy | | |
9495
| 404 | Sum of Left Leaves | Easy | O(N) | O(H) | Tree | | |
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#
2+
# @lc app=leetcode id=395 lang=python3
3+
#
4+
# [395] Longest Substring with At Least K Repeating Characters
5+
#
6+
7+
# @lc code=start
8+
# TAGS: Divide and Conquer, Recursion, Sliding Window
9+
class Solution:
10+
# LTE. Time: O(N^2)
11+
def longestSubstring(self, s: str, k: int) -> int:
12+
def valid(counter, k):
13+
return all(c >= k for c in counter.values())
14+
15+
ans = 0
16+
for i in range(len(s)):
17+
counter = collections.Counter()
18+
for j in range(i, len(s)):
19+
counter[s[j]] += 1
20+
if valid(counter, k):
21+
ans = max(ans, j - i + 1)
22+
return ans
23+
24+
# Divide and conquer. 32 ms, 83.66%.
25+
# Worst case is still O(N^2) but O(NlogN) on average
26+
def longestSubstring(self, s: str, k: int) -> int:
27+
for c in set(s):
28+
if s.count(c) < k: # split here
29+
return max(self.longestSubstring(sub, k) for sub in s.split(c))
30+
return len(s)
31+
# @lc code=end
32+

testcases/395

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"aaabb"
2+
3
3+
"ababbc"
4+
2
5+
"abaaaccaabb"
6+
3
7+
"a"
8+
1
9+
"aabbaaabbccddeeaa"
10+
3
11+
"baaaccaaabdd"
12+
2
13+
"baaacccaaabdd"
14+
3
15+
"aa"
16+
3
17+
"baaabcb"
18+
3

0 commit comments

Comments
 (0)