From bfe3c82c399d20af2c42ff05b5e747bb6ec0e0fd Mon Sep 17 00:00:00 2001 From: Ben Makusha Date: Sat, 15 Nov 2025 22:34:09 -0500 Subject: [PATCH] chore: [LEETCODE 1513] Number of Substrings With Only 1s <> Solution --- .../a1513/NumberOfSubstringsWithOnlyOnes.java | 19 ++++++++++ .../NumberOfSubstringsWithOnlyOnesTest.java | 38 +++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 src/main/java/problemset/a1513/NumberOfSubstringsWithOnlyOnes.java create mode 100644 src/test/java/problemset/a1513/NumberOfSubstringsWithOnlyOnesTest.java diff --git a/src/main/java/problemset/a1513/NumberOfSubstringsWithOnlyOnes.java b/src/main/java/problemset/a1513/NumberOfSubstringsWithOnlyOnes.java new file mode 100644 index 0000000..63d5682 --- /dev/null +++ b/src/main/java/problemset/a1513/NumberOfSubstringsWithOnlyOnes.java @@ -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; + } +} diff --git a/src/test/java/problemset/a1513/NumberOfSubstringsWithOnlyOnesTest.java b/src/test/java/problemset/a1513/NumberOfSubstringsWithOnlyOnesTest.java new file mode 100644 index 0000000..34b58f2 --- /dev/null +++ b/src/test/java/problemset/a1513/NumberOfSubstringsWithOnlyOnesTest.java @@ -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)); + } +} \ No newline at end of file