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

Commita1fada8

Browse files
add a solution for 395
1 parentcff8014 commita1fada8

File tree

2 files changed

+101
-5
lines changed

2 files changed

+101
-5
lines changed

‎src/main/java/com/fishercoder/solutions/_395.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,63 @@ int findLongestSubstring(char[] chars, int start, int end, int k) {
3737
returnend -start;
3838
}
3939
}
40+
41+
publicstaticclassSolution2 {
42+
/**
43+
* classic sliding window approach.
44+
* credit: https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/discuss/170010/Java-O(n)-Solution-with-Detailed-Explanation-Sliding-Window/774350
45+
*/
46+
publicintlongestSubstring(Strings,intk) {
47+
intres =0;
48+
for (intnumUniqueTarget =1;numUniqueTarget <=26;numUniqueTarget++) {
49+
res =Math.max(res,slidingWindowHelper(s,k,numUniqueTarget));
50+
}
51+
returnres;
52+
}
53+
54+
// sliding window template
55+
privateintslidingWindowHelper(Strings,intk,intnumUniqueTarget) {
56+
int[]map =newint[26];
57+
intstart =0;
58+
intend =0;
59+
intres =0;
60+
intuniqueLetterCount =0;
61+
intnumNoLessThanK =0;
62+
while (end <s.length()) {
63+
charc1 =s.charAt(end);
64+
if (map[c1 -'a'] ==0) {
65+
//we increment this when we include a new letter into our sliding window
66+
uniqueLetterCount++;
67+
}
68+
map[c1 -'a']++;
69+
if (map[c1 -'a'] ==k) {
70+
//we increment this number when we find a letter's frequency reaches k
71+
numNoLessThanK++;
72+
}
73+
end++;
74+
75+
while (uniqueLetterCount >numUniqueTarget) {
76+
//as long as the counter (the number of qualified letters) is greater than our target number,
77+
//we can move the left pointer to the right,
78+
//this keeps the interval within our sliding window always valid
79+
charc2 =s.charAt(start);
80+
if (map[c2 -'a'] ==k) {
81+
//we decrement this numNoLessThanK when we find this letter's frequency equals
82+
//to k because we'll move past this letter, i.e. our sliding window no longer includes it
83+
numNoLessThanK--;
84+
}
85+
map[c2 -'a']--;
86+
if (map[c2 -'a'] ==0) {
87+
uniqueLetterCount--;
88+
}
89+
start++;
90+
}
91+
92+
if (uniqueLetterCount ==numNoLessThanK) {
93+
res =Math.max(res,end -start);
94+
}
95+
}
96+
returnres;
97+
}
98+
}
4099
}

‎src/test/java/com/fishercoder/_395Test.java

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,63 @@
66

77
importstaticjunit.framework.Assert.assertEquals;
88

9-
/**
10-
* Created by fishercoder on 12/31/16.
11-
*/
129
publicclass_395Test {
1310

1411
privatestatic_395.Solution1solution1;
12+
privatestatic_395.Solution2solution2;
13+
privatestaticStrings;
14+
privatestaticintk;
15+
privatestaticintexpected;
1516

1617
@BeforeClass
1718
publicstaticvoidsetup() {
1819
solution1 =new_395.Solution1();
20+
solution2 =new_395.Solution2();
1921
}
2022

2123
@Test
2224
publicvoidtest1() {
23-
assertEquals(5,solution1.longestSubstring("ababbc",2));
25+
s ="ababbc";
26+
expected =5;
27+
k =2;
28+
assertEquals(expected,solution1.longestSubstring(s,k));
29+
assertEquals(expected,solution2.longestSubstring(s,k));
2430
}
2531

2632
@Test
2733
publicvoidtest2() {
28-
assertEquals(3,solution1.longestSubstring("aaabb",3));
34+
s ="aaabb";
35+
k =3;
36+
expected =3;
37+
assertEquals(expected,solution1.longestSubstring(s,k));
38+
assertEquals(expected,solution2.longestSubstring(s,k));
39+
}
40+
41+
@Test
42+
publicvoidtest3() {
43+
s ="bbaaacbd";
44+
k =3;
45+
expected =3;
46+
assertEquals(expected,solution1.longestSubstring(s,k));
47+
assertEquals(expected,solution2.longestSubstring(s,k));
48+
}
49+
50+
@Test
51+
publicvoidtest4() {
52+
s ="weitong";
53+
k =2;
54+
expected =0;
55+
assertEquals(expected,solution1.longestSubstring(s,k));
56+
assertEquals(expected,solution2.longestSubstring(s,k));
57+
}
58+
59+
@Test
60+
publicvoidtest5() {
61+
s ="a";
62+
k =2;
63+
expected =0;
64+
assertEquals(expected,solution1.longestSubstring(s,k));
65+
assertEquals(expected,solution2.longestSubstring(s,k));
2966
}
3067

3168
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp