If there are more than 3 letters together (i.e. next to each other) from the given Alphabet Set: {a,g,w,k,l}, then your string is “BAD”. If a number is repeated more than three times, then your string is “BAD”. Print "1” if string is GOOD, else print "0".
For the first condition, I am able to do it. But for the second condition, I am having problem with converting a string to an int. I tried using stoi() but I think I am not using it correctly.
#include<bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--> 0) {
string s;
cin >> s;
int ccount = 0;
int arr[10] = {0,0,0,0,0,0,0,0,0,0};
bool b = true;
for (int i = 0; i < s.size(); i++) {
if (s[i] == 'a' || s[i] == 'g' || s[i] == 'w' || s[i] == 'k' || s[i] == 'l') {
ccount++;
if (ccount >= 3) {
b = false;
break;
}
} else if (isdigit(s[i])) {
ccount = 0;
int num = stoi( & s, sizeof(s) * i, 10);
arr[num]++;
int k = 0;
for (k = 0; k < 10; k++) {
if (arr[k] >= 3) {
b = false;
break;
}
}
} else {
ccount = 0;
}
}
if (b == true) {
cout << 1 << endl;
} else {
cout << 0 << endl;
}
}
return 0;
}
Input:
3
qw2uha
awkl5
y2y2y2y2
Output:
1
0
0
EDIT: ASCII conversion works very well.
I have a not so important problem, I know we can't directly compare integers of different signedness, but still I am curious.
#include<bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--> 0) {
string s;
cin >> s;
int ccount = 0;
int arr[10] = {0,0,0,0,0,0,0,0,0,0};
bool b = true;
for (int i = 0; i < s.size(); i++) {
if (s[i] == 'a' || s[i] == 'g' || s[i] == 'w' || s[i] == 'k' || s[i] == 'l') {
ccount++;
if (ccount >= 3) {
b = false;
break;
}
} else if (isdigit(s[i])) {
ccount = 0;
int num = s[i] - '0';
arr[num]++;
int k = 0;
for (k = 0; k < 10; k++) {
if (arr[k] > 3) {
b = false;
break;
}
}
} else {
ccount = 0;
}
}
if (b == true) {
cout << 1 << endl;
} else {
cout << 0 << endl;
}
}
return 0;
}
I am getting this warning :
14:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'unsigned int'} [-Wsign-compare]
14 | for (int i = 0; i < s.size(); i++) {
| ~~^~~~~~~~~~
]
Is there any way to fix the warning?
