Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit50cf51e

Browse files
add 395
1 parente158815 commit50cf51e

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ Your ideas/fixes/algorithms are more than welcome!
242242
|398|[Random Pick Index](https://leetcode.com/problems/random-pick-index/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_398.java) | | | Medium| Reservoir Sampling
243243
|397|[Integer Replacement](https://leetcode.com/problems/integer-replacement/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_397.java)| ? | ? | Easy| BFS
244244
|396|[Rotate Function](https://leetcode.com/problems/rotate-function/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_396.java)| O(n^2) could be optimized to O(n)| O(1)| Easy|
245+
|395|[Longest Substring with At Least K Repeating Characters](https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_395.java)| O(n^2) | O(1) | Medium| Recursion
245246
|393|[UTF-8 Validation](https://leetcode.com/problems/utf-8-validation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_393.java)| O(?)|O(?) | Medium| Bit Manipulation
246247
|392|[Is Subsequence](https://leetcode.com/problems/is-subsequence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_392.java)| O(m*n)|O(1) | Medium| Array, String
247248
|391|[Perfect Rectangle](https://leetcode.com/problems/perfect-rectangle/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_391.java)| O(n)|O(1)| Hard|
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
packagecom.fishercoder.solutions;
2+
3+
/**
4+
* 395. Longest Substring with At Least K Repeating Characters
5+
*
6+
* Find the length of the longest substring T of a given string
7+
* (consists of lowercase letters only)
8+
* such that every character in T appears no less than k times.
9+
10+
Example 1:
11+
Input:
12+
s = "aaabb", k = 3
13+
14+
Output:
15+
3
16+
17+
The longest substring is "aaa", as 'a' is repeated 3 times.
18+
19+
20+
Example 2:
21+
Input:
22+
s = "ababbc", k = 2
23+
24+
Output:
25+
5
26+
27+
The longest substring is "ababb", as 'a' is repeated 2 times and 'b' is repeated 3 times.
28+
*/
29+
30+
publicclass_395 {
31+
publicstaticclassSolution1 {
32+
/**Reference: https://discuss.leetcode.com/topic/57372/java-divide-and-conquer-recursion-solution*/
33+
publicintlongestSubstring(Strings,intk) {
34+
returnfindLongestSubstring(s.toCharArray(),0,s.length(),k);
35+
}
36+
37+
intfindLongestSubstring(char[]chars,intstart,intend,intk) {
38+
if (end -start <k) {
39+
return0;
40+
}
41+
int[]count =newint[26];
42+
for (inti =start;i <end;i++) {
43+
intindex =chars[i] -'a';
44+
count[index]++;
45+
}
46+
47+
for (inti =0;i <26;i++) {
48+
if (count[i] <k &&count[i] >0) {
49+
for (intj =start;j <end;j++) {
50+
if (chars[j] ==i +'a') {
51+
intleft =findLongestSubstring(chars,start,j,k);
52+
intright =findLongestSubstring(chars,j +1,end,k);
53+
returnMath.max(left,right);
54+
}
55+
}
56+
}
57+
}
58+
returnend -start;
59+
}
60+
}
61+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._395;
4+
importorg.junit.BeforeClass;
5+
importorg.junit.Test;
6+
7+
importstaticjunit.framework.Assert.assertEquals;
8+
9+
/**
10+
* Created by fishercoder on 12/31/16.
11+
*/
12+
publicclass_395Test {
13+
14+
privatestatic_395.Solution1solution1;
15+
16+
@BeforeClass
17+
publicstaticvoidsetup() {
18+
solution1 =new_395.Solution1();
19+
}
20+
21+
@Test
22+
publicvoidtest1() {
23+
assertEquals(5,solution1.longestSubstring("ababbc",2));
24+
}
25+
26+
@Test
27+
publicvoidtest2() {
28+
assertEquals(3,solution1.longestSubstring("aaabb",3));
29+
}
30+
31+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp