Skip to content

Commit 50cf51e

Browse files
add 395
1 parent e158815 commit 50cf51e

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+
package com.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+
public class _395 {
31+
public static class Solution1 {
32+
/**Reference: https://discuss.leetcode.com/topic/57372/java-divide-and-conquer-recursion-solution*/
33+
public int longestSubstring(String s, int k) {
34+
return findLongestSubstring(s.toCharArray(), 0, s.length(), k);
35+
}
36+
37+
int findLongestSubstring(char[] chars, int start, int end, int k) {
38+
if (end - start < k) {
39+
return 0;
40+
}
41+
int[] count = new int[26];
42+
for (int i = start; i < end; i++) {
43+
int index = chars[i] - 'a';
44+
count[index]++;
45+
}
46+
47+
for (int i = 0; i < 26; i++) {
48+
if (count[i] < k && count[i] > 0) {
49+
for (int j = start; j < end; j++) {
50+
if (chars[j] == i + 'a') {
51+
int left = findLongestSubstring(chars, start, j, k);
52+
int right = findLongestSubstring(chars, j + 1, end, k);
53+
return Math.max(left, right);
54+
}
55+
}
56+
}
57+
}
58+
return end - start;
59+
}
60+
}
61+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._395;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static junit.framework.Assert.assertEquals;
8+
9+
/**
10+
* Created by fishercoder on 12/31/16.
11+
*/
12+
public class _395Test {
13+
14+
private static _395.Solution1 solution1;
15+
16+
@BeforeClass
17+
public static void setup() {
18+
solution1 = new _395.Solution1();
19+
}
20+
21+
@Test
22+
public void test1() {
23+
assertEquals(5, solution1.longestSubstring("ababbc", 2));
24+
}
25+
26+
@Test
27+
public void test2() {
28+
assertEquals(3, solution1.longestSubstring("aaabb", 3));
29+
}
30+
31+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy