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

Commit 4665ffa

Browse files
add 3258
1 parent c04ffdc commit 4665ffa

File tree

3 files changed

+58
-0
lines changed
  • paginated_contents/algorithms/4th_thousand
  • src

3 files changed

+58
-0
lines changed

‎paginated_contents/algorithms/4th_thousand/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
| # | Title | Solutions | Video | Difficulty | Tag
22
|------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------|------------|----------------------------------------------------------------------
3+
| 3258 | [Count Substrings That Satisfy K-Constraint I](https://leetcode.com/problems/count-substrings-that-satisfy-k-constraint-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3258.java) | | Easy |
34
| 3254 | [Find the Power of K-Size Subarrays I](https://leetcode.com/problems/find-the-power-of-k-size-subarrays-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3254.java) | | Easy |
45
| 3249 | [Count the Number of Good Nodes](https://leetcode.com/problems/count-the-number-of-good-nodes/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3249.java) | | Medium |
56
| 3248 | [Snake in Matrix](https://leetcode.com/problems/snake-in-matrix/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3248.java) | | Easy |
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.fishercoder.solutions.fourththousand;
2+
3+
public class _3258 {
4+
public static class Solution1 {
5+
public int countKConstraintSubstrings(String s, int k) {
6+
int ans = 0;
7+
for (int i = 0; i < s.length(); i++) {
8+
for (int j = i + 1; j <= s.length(); j++) {
9+
String candidate = s.substring(i, j);
10+
if (meetConstraints(candidate, k)) {
11+
ans++;
12+
}
13+
}
14+
}
15+
return ans;
16+
}
17+
18+
private boolean meetConstraints(String candidate, int k) {
19+
int ones = 0;
20+
int zeroes = 0;
21+
for (char c : candidate.toCharArray()) {
22+
if (c == '0') {
23+
zeroes++;
24+
} else {
25+
ones++;
26+
}
27+
}
28+
return ones <= k || zeroes <= k;
29+
}
30+
}
31+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.fishercoder.fourththousand;
2+
3+
import com.fishercoder.solutions.fourththousand._3258;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
9+
public class _3258Test {
10+
private _3258.Solution1 solution1;
11+
12+
@BeforeEach
13+
public void setup() {
14+
solution1 = new _3258.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(12, solution1.countKConstraintSubstrings("10101", 1));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(25, solution1.countKConstraintSubstrings("1010101", 2));
25+
}
26+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /