-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathProblem_12.java
More file actions
42 lines (34 loc) · 1.09 KB
/
Problem_12.java
File metadata and controls
42 lines (34 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package strings;
import java.util.*;
// Problem Title => Split the Binary string into two substring with equal 0’s and 1’s
public class Problem_12 {
// Function to return the count of maximum substrings str can be divided into
static int maxSubStr(String str, int n) {
// To store the count of 0s and 1s
int count0 = 0, count1 = 0;
// To store the count of maximum substrings str can be divided into
int cnt = 0;
for (int i = 0; i < n; i++) {
if (str.charAt(i) == 0)
count0++;
else
count1++;
if (count0 == count1)
cnt++;
}
// it is not possible to split the string
if (cnt == 0)
return -1;
return cnt;
}
// Driver Code
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String str = sc.nextLine();
sc.close();
System.out.println(maxSubStr(str, n));
}
}
// Time Complexity => O(n) {Linear}
// Space Complexity => O(1) {constant}