Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/main/java/problemset/a1513/NumberOfSubstringsWithOnlyOnes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package problemset.a1513;

public class NumberOfSubstringsWithOnlyOnes {
final int MODULO = (int) 1e9 + 7;

public int numSub(String s) {
int res = 0, consecutiveOnes = 0;

for (char c : s.toCharArray()) {
if (c == '1')
consecutiveOnes++;
else
consecutiveOnes = 0;

res = (res + consecutiveOnes) % MODULO;
}
return res;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package problemset.a1513;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class NumberOfSubstringsWithOnlyOnesTest {
private NumberOfSubstringsWithOnlyOnes numSubstrings;

private String testcaseOne;
private String testcaseTwo;
private String testcaseThree;

@BeforeEach
void setUp() {
numSubstrings = new NumberOfSubstringsWithOnlyOnes();

testcaseOne = "0110111";
testcaseTwo = "101";
testcaseThree = "111111";
}

@Test
void test_numSub_testCaseOne() {
assertEquals(9, numSubstrings.numSub(testcaseOne));
}

@Test
void test_numSub_testCaseTwo() {
assertEquals(2, numSubstrings.numSub(testcaseTwo));
}

@Test
void test_numSub_testCaseThree() {
assertEquals(21, numSubstrings.numSub(testcaseThree));
}
}
Loading