-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsameonezerostring.java
More file actions
39 lines (37 loc) · 1.01 KB
/
sameonezerostring.java
File metadata and controls
39 lines (37 loc) · 1.01 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
import java.util.HashMap;
import java.util.Map;
public class sameonezerostring {
private static int n = 100;
public static void main(String[] args) {
String s = "111000110";
sameonezerostring same = new sameonezerostring();
System.out.println(same.n);
same.setN(200);
System.out.println(n);
System.out.println(helper(s));
}
public static int helper(String s) {
int count = 0;
int max = 0;
Map<Integer, Integer> map = new HashMap<>();
map.put(0, -1);
for(int i = 0; i < s.length(); i++) {
if(s.charAt(i) == '1') count++;
else count--;
if(!map.containsKey(count)) {
map.put(count, i);
}
else {
int temp = i - map.get(count);
if (temp > max) max = temp;
}
}
return max;
}
public static int helper(){
return 0;
}
public void setN(int n ) {
this.n = n;
}
}