Split Binary String Problem Statement

Chintu has a long binary string str. A binary string is a string that contains only 0 and 1. He considers a string to be 'beautiful' if and only if the number of 0's and 1's in the string are equal.

Example:

Beautiful Strings:
0011, 1100, 101010
Non-Beautiful Strings:
1110, 0001, 10101

Now, Chintu wants to split the string into substrings such that each substring is beautiful. Can you help Chintu to find the maximum number of beautiful strings he can split the string into? If it is not possible to split the string in such a way that all strings are beautiful, return -1.

Example:

Input:
str = "101001"
Output:
3
Explanation:

The string can be divided into 3 beautiful strings: "10", "10", and "01".

Input:

The first line contains an integer 'T' denoting the number of test cases to run. Then the test cases follow.
The first line of each test case contains the string 'str'.

Output:

For each test case, return the maximum number of substrings that 'str' can be split into such that each substring is beautiful.
If there are none, return -1.
Output for each test case will be printed in a new line.

Constraints:

  • 1 <= T <= 100
  • 1 <= N <= 5000
  • Time limit: 1 second
Note:
You do not need to print anything; it has already been taken care of. Just implement the given function.
AnswerBot
4mo

Find the maximum number of beautiful substrings that a binary string can be split into.

  • Count the number of 0's and 1's in the string.

  • Iterate through the string and split it whenever the count of 0's a...read more

Help your peers!
Select
Add answer anonymously...

JPMorgan Chase & Co. Software Developer interview questions & answers

A Software Developer was asked 12mo agoQ. Given a Python program, debug the program.
A Software Developer was asked Q. What are the differences between a HashMap and a LinkedHashMap?
A Software Developer was asked Q. Given an array of integers nums and an integer target, return indices of the two...read more

Popular interview questions of Software Developer

A Software Developer was asked 12mo agoQ1. Given a Python program, debug the program.
A Software Developer was asked Q2. What are the differences between a HashMap and a LinkedHashMap?
A Software Developer was asked Q3. Given an array of integers nums and an integer target, return indices of the two...read more
JPMorgan Chase & Co. Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits