Skip to content

Commit e3ca3eb

Browse files
authored
Create number-of-substrings-with-only-1s.cpp
1 parent 9448ffd commit e3ca3eb

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
int numSub(string s) {
7+
static const int MOD = 1e9 + 7;
8+
int result = 0, count = 0;
9+
for (const auto& c : s) {
10+
count = (c == '1') ? count + 1 : 0;
11+
result = (result + count) % MOD;
12+
}
13+
return result;
14+
}
15+
};

0 commit comments

Comments
 (0)