LeetCode 1573: Number of Ways to Split a String Solution

Master LeetCode problem 1573 (Number of Ways to Split a String), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1573. Number of Ways to Split a String

Problem Explanation

Explanation:

To solve this problem, we can iterate through the binary string s and keep track of the total number of ones in the string. Then, we can find all possible ways to split the string into 3 parts such that each part contains the same number of ones. We can achieve this by counting the number of zeros between each pair of ones and calculate the total number of valid splits.

  1. Iterate through the string s and count the total number of ones.
  2. If the total number of ones is not divisible by 3, return 0 as it is not possible to split the string into 3 parts with the same number of ones.
  3. Calculate the required number of ones in each part (targetOnes) by dividing the total number of ones by 3.
  4. Initialize variables to keep track of the number of zeros between each pair of ones and the total number of ways to split the string.
  5. Iterate through the string s again and count the number of zeros between each pair of ones. Increment the count of zeros and if the count reaches the targetOnes, reset the count and increment the total number of ways.
  6. Return the total number of ways modulo 10^9 + 7.

Time Complexity: O(n) where n is the length of the input string s.

Space Complexity: O(1)

:

Solution Code

class Solution {
    public int numWays(String s) {
        int ones = 0;
        for (char c : s.toCharArray()) {
            if (c == '1') ones++;
        }
        if (ones % 3 != 0) return 0;
        
        int targetOnes = ones / 3;
        if (targetOnes == 0) {
            long ways = (long)(s.length() - 1) * (s.length() - 2) / 2;
            return (int)(ways % (1e9 + 7));
        }
        
        int countZeros = 0;
        long totalWays = 0;
        for (char c : s.toCharArray()) {
            if (c == '0') {
                countZeros++;
            } else {
                if (countZeros == targetOnes) {
                    totalWays++;
                    countZeros = 0;
                }
            }
        }
        return (int)(totalWays % (1e9 + 7));
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1573 (Number of Ways to Split a String)?

This page provides optimized solutions for LeetCode problem 1573 (Number of Ways to Split a String) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1573 (Number of Ways to Split a String)?

The time complexity for LeetCode 1573 (Number of Ways to Split a String) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1573 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1573 in Java, C++, or Python.

Back to LeetCode Solutions