Skip to content

Commit a45e6ec

Browse files
add 1100
1 parent fecc631 commit a45e6ec

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ _If you like this project, please leave me a star._ ★
8888
|1108|[Defanging an IP Address](https://leetcode.com/problems/defanging-an-ip-address/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1108.java) | [:tv:](https://www.youtube.com/watch?v=FP0Na-pL0qk)|Easy||
8989
|1104|[Path In Zigzag Labelled Binary Tree](https://leetcode.com/problems/path-in-zigzag-labelled-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1104.java) | |Medium|Math, Tree|
9090
|1103|[Distribute Candies to People](https://leetcode.com/problems/distribute-candies-to-people/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1103.java) | |Easy|Math|
91+
|1100|[Find K-Length Substrings With No Repeated Characters](https://leetcode.com/problems/find-k-length-substrings-with-no-repeated-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1100.java) | |Medium|String, Sliding Window|
9192
|1099|[Two Sum Less Than K](https://leetcode.com/problems/two-sum-less-than-k/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1099.java) | [:tv:](https://www.youtube.com/watch?v=2Uq7p7HE0TI)|Easy||
9293
|1089|[Duplicate Zeros](https://leetcode.com/problems/duplicate-zeros/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1089.java) | |Easy||
9394
|1086|[High Five](https://leetcode.com/problems/high-five/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1086.java) | [:tv:](https://www.youtube.com/watch?v=3iqC5J4l0Cc)|Easy||
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
/**
7+
* 1100. Find K-Length Substrings With No Repeated Characters
8+
*
9+
* Given a string S, return the number of substrings of length K with no repeated characters.
10+
*
11+
* Example 1:
12+
* Input: S = "havefunonleetcode", K = 5
13+
* Output: 6
14+
* Explanation:
15+
* There are 6 substrings they are : 'havef','avefu','vefun','efuno','etcod','tcode'.
16+
*
17+
* Example 2:
18+
* Input: S = "home", K = 5
19+
* Output: 0
20+
* Explanation:
21+
* Notice K can be larger than the length of S. In this case is not possible to find any substring.
22+
*
23+
* Note:
24+
* 1 <= S.length <= 10^4
25+
* All characters of S are lowercase English letters.
26+
* 1 <= K <= 10^4
27+
* */
28+
public class _1100 {
29+
public static class Solution1 {
30+
public int numKLenSubstrNoRepeats(String S, int K) {
31+
int count = 0;
32+
Set<Character> set = new HashSet<>();
33+
for (int i = 0; i <= S.length() - K; i++) {
34+
String string = S.substring(i, i+K);
35+
boolean invalid = false;
36+
for (char c : string.toCharArray()) {
37+
if (!set.add(c)) {
38+
invalid = true;
39+
break;
40+
}
41+
}
42+
count += invalid ? 0 : 1;
43+
set.clear();
44+
}
45+
return count;
46+
}
47+
}
48+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1100;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1100Test {
10+
private static _1100.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1100.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(6, solution1.numKLenSubstrNoRepeats("havefunonleetcode", 5));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(0, solution1.numKLenSubstrNoRepeats("home", 5));
25+
}
26+
}

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