Leetcode Problem 1513. Number of Substrings With Only 1s

1513. Number of Substrings With Only 1s

Leetcode Solutions

Counting Consecutive Ones

  1. Initialize a variable count to keep track of the length of consecutive '1's.
  2. Initialize a variable result to store the final count of substrings.
  3. Iterate through each character in the string s.
  4. If the current character is '1', increment count.
  5. If the current character is '0' or it is the last character in the string, calculate the number of substrings using the formula count * (count + 1) / 2, add it to result, and reset count to 0.
  6. Take the modulo of result with 1e9 + 7 after each addition.
  7. Return result as the final answer.
UML Thumbnail

Sliding Window to Count Consecutive Ones

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...