Skip to content

Commit d7a6455

Browse files
add 3234
1 parent f18f8fd commit d7a6455

File tree

3 files changed

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

3 files changed

+64
-0
lines changed

Diff for: paginated_contents/algorithms/4th_thousand/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
| # | Title | Solutions | Video | Difficulty | Tag
22
|------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------|------------|----------------------------------------------------------------------
3+
| 3234 | [Count the Number of Substrings With Dominant Ones](https://leetcode.com/problems/count-the-number-of-substrings-with-dominant-ones/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3234.java) | | Medium | Sliding Window
34
| 3233 | [Find the Count of Numbers Which Are Not Special](https://leetcode.com/problems/find-the-count-of-numbers-which-are-not-special/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3233.java) | | Medium | Math
45
| 3232 | [Find if Digit Game Can Be Won](https://leetcode.com/problems/find-if-digit-game-can-be-won/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3232.java) | | Easy |
56
| 3226 | [Number of Bit Changes to Make Two Integers Equal](https://leetcode.com/problems/number-of-bit-changes-to-make-two-integers-equal/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3226.java) | | Easy |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.fishercoder.solutions.fourththousand;
2+
3+
public class _3234 {
4+
public static class Solution1 {
5+
/**
6+
* Sliding window.
7+
* credit: https://leetcode.com/problems/count-the-number-of-substrings-with-dominant-ones/solutions/5547005/sliding-window-java-o-sqrt-of-n-n/
8+
* The idea is:
9+
* 1. we fix the number of zeroes in each iteration, then the number of ones is zeroes * zeroes;
10+
* 2. now we operate the sliding window.
11+
*/
12+
public int numberOfSubstrings(String s) {
13+
int ans = 0;
14+
for (int zeroes = 0; zeroes * zeroes < s.length(); zeroes++) {
15+
int[] count = new int[2];
16+
int lastPos = -1;
17+
//end keeps moving to the right in each iteration
18+
for (int start = 0, end = 0; end < s.length(); end++) {
19+
count[s.charAt(end) - '0']++;
20+
while (start < end) {
21+
if (s.charAt(start) == '0' && count[0] > zeroes) {
22+
//this means we have more zeroes than we want, so we'll move start to the right by one
23+
count[0]--;
24+
lastPos = start;
25+
} else if (s.charAt(start) == '1' && (count[1] - 1) >= (zeroes * zeroes)) {
26+
//this means the current start position is '1' and after excluding it, the window is still a valid dominant one
27+
count[1]--;
28+
} else {
29+
break;
30+
}
31+
start++;
32+
}
33+
if (count[0] == zeroes && count[1] >= zeroes * zeroes) {
34+
ans += (start - lastPos);
35+
}
36+
}
37+
}
38+
return ans;
39+
}
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.fishercoder.fourththousand;
2+
3+
import com.fishercoder.solutions.fourththousand._3234;
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 _3234Test {
10+
private static _3234.Solution1 solution1;
11+
12+
@BeforeEach
13+
public void setup() {
14+
solution1 = new _3234.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(5, solution1.numberOfSubstrings("00011"));
20+
}
21+
22+
}

0 commit comments

Comments
 (0)