Skip to content

Commit 8939c20

Browse files
committed
codechef GOODBINSTR
1 parent 74b3c26 commit 8939c20

File tree

2 files changed

+90
-59
lines changed

2 files changed

+90
-59
lines changed

Codechef/GOODBINSTR.cpp

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
written by Pankaj Kumar.
3+
country:-INDIA
4+
*/
5+
#include <bits/stdc++.h>
6+
using namespace std;
7+
typedef long long ll;
8+
9+
10+
int solve(){
11+
string s;
12+
cin>>s;
13+
int n=s.size();
14+
int count=0;
15+
16+
int oneZero=0,zeroOne=0;
17+
for(int i=1;i<n;i++){
18+
if(s[i-1]=='0' && s[i]=='1'){
19+
zeroOne++;
20+
}
21+
else if(s[i-1]=='1' && s[i]=='0'){
22+
oneZero++;
23+
}
24+
}
25+
for(int i=0;i<n;i++){
26+
int tempZeroOne=zeroOne,tempOneZero=oneZero;
27+
if(i-1>=0){
28+
if(s[i-1]=='1'){
29+
// 11 -> 10
30+
if(s[i]=='1'){
31+
tempOneZero++;
32+
}
33+
// 10 -> 11
34+
else{
35+
tempOneZero--;
36+
}
37+
}
38+
else{
39+
// 01 -> 00
40+
if (s[i] == '1')
41+
{
42+
tempZeroOne--;
43+
}
44+
// 00 -> 01
45+
else
46+
{
47+
tempZeroOne++;
48+
}
49+
}
50+
}
51+
if(i+1<n){
52+
if(s[i]=='1'){
53+
// 11 -> 01
54+
if(s[i+1]=='1'){
55+
tempZeroOne++;
56+
}
57+
// 10 -> 00
58+
else{
59+
tempOneZero--;
60+
}
61+
}
62+
else{
63+
// 01 -> 11
64+
if (s[i + 1] == '1')
65+
{
66+
tempZeroOne--;
67+
}
68+
// 00 -> 10
69+
else
70+
{
71+
tempOneZero++;
72+
}
73+
}
74+
}
75+
if(tempZeroOne==tempOneZero){
76+
count++;
77+
}
78+
}
79+
cout<<count<<endl;
80+
return 0;
81+
}
82+
int main()
83+
{
84+
int testCase=1;
85+
cin>>testCase;
86+
while(testCase--){
87+
solve();
88+
}
89+
return 0;
90+
}

LeetCode/59.Sprial_Matrix_II.cpp

Lines changed: 0 additions & 59 deletions
This file was deleted.

0 commit comments

Comments
 (0)