File tree Expand file tree Collapse file tree 2 files changed +61
-0
lines changed Expand file tree Collapse file tree 2 files changed +61
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int countBinarySubstrings (String s ) {
3
+ int prev = 0 ;
4
+ int curr = 1 ;
5
+ int res = 0 ;
6
+
7
+ for (int i =1 ;i <s .length ();i ++) {
8
+ if (s .charAt (i ) == s .charAt (i -1 )) curr ++;
9
+ else {
10
+ prev = curr ;
11
+ curr = 1 ;
12
+ }
13
+ if (prev >= curr ) res ++;
14
+ }
15
+ return res ;
16
+ }
17
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int findShortestSubArray (int [] nums ) {
3
+
4
+ int degree = 0 ;
5
+ Map <Integer , Integer > map = new HashMap <>();
6
+ Map <Integer , ArrayList <Integer >> mapCheck = new HashMap <>();
7
+
8
+ for (int i =0 ;i <nums .length ;i ++) {
9
+ if (map .containsKey (nums [i ])) {
10
+ map .put (nums [i ],map .get (nums [i ])+1 );
11
+ }
12
+ else {
13
+ map .put (nums [i ],1 );
14
+ }
15
+ degree = Math .max (degree ,map .get (nums [i ]));
16
+
17
+ if (mapCheck .containsKey (nums [i ])) {
18
+ ArrayList <Integer > arr = new ArrayList <>();
19
+ arr .add (mapCheck .get (nums [i ]).get (0 ));
20
+ arr .add (i );
21
+ mapCheck .put (nums [i ], arr );
22
+ }
23
+ else {
24
+ ArrayList <Integer > arr = new ArrayList <>();
25
+ arr .add (i );
26
+ arr .add (i );
27
+ mapCheck .put (nums [i ], arr );
28
+ }
29
+ }
30
+
31
+ int minSize = Integer .MAX_VALUE ;
32
+
33
+ for (Map .Entry <Integer , Integer > entry : map .entrySet ()) {
34
+ if (entry .getValue () == degree ) {
35
+ int start = mapCheck .get (entry .getKey ()).get (0 );
36
+ int end = mapCheck .get (entry .getKey ()).get (1 );
37
+
38
+ minSize = Math .min (end -start +1 , minSize );
39
+ }
40
+ }
41
+
42
+ return minSize ;
43
+ }
44
+ }
You can’t perform that action at this time.
0 commit comments