-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcount_even_substrings.cpp
83 lines (49 loc) · 1.15 KB
/
count_even_substrings.cpp
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <bits/stdc++.h>
using namespace std;
//O(n2) Generate all the substrings and individually check them.
int check_even(string s)
{
if((s[s.length()-1]-'0')%2==0) return 1;
else return 0;
}
int main() {
int t;
cin>>t;
string s;
while(t!=0)
{
cin>>s;
int l=s.length(),cnt=0,i,j;
for(i=0;i<l;i++)
{
for(j=1;j<=l-i;j++)
{
if(check_even(s.substr(i,j))) cnt++;
}
}
cout<<cnt<<endl;
t--;
}
return 0;
}
****************************************************************************************************************************
//O(n) approach is to add the occurence of any even bit which is index+1 times
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin>>t;
string s;
while(t!=0)
{
cin>>s;
int l=s.length(),cnt=0,i,j;
for(i=0;i<l;i++)
{
if((s[i]-'0')%2==0) cnt=cnt+(i+1);
}
cout<<cnt<<endl;
t--;
}
return 0;
}