PASSWORD HELP

#include <bits/stdc++.h>
#include
#include
#include
#include

#define ll long long int
#include
#define mod 1000000007
using namespace std;
void get_array(ll array[],ll size)
{
for(ll i=0;i<size;i++)
{
cin>>array[i];
}
}
void put_array(ll array[],ll size)
{
for(ll i=0;i<size;i++)
{
cout<<array[i]<<" ";
}
}
int main()
{
ll tc;
cin>>tc;
while(tc–)
{
int lower_case=-1,upper_case=-1,number=-1,s_c=-1;
string s;
cin>>s;
if(s.length()<10)
{
cout<<“NO”<<endl;
}
else if(s[0]==‘A’||s[s.length()-1]==‘Z’||s[0]==‘Z’||s[s.length()-1]==‘A’)
{
cout<<“NO”<<endl;
}
else if(s[0]>=‘0’&&s[0]<=‘9’||s[s.length()-1]>=‘0’&&s[s.length()-1]<=‘9’)
{
cout<<“NO”<<endl;
}
else if(s[0]==’@’||s[0]==’#’||s[0]==’%’||s[0]==’&’||s[0]==’?’||s[s.length()-1]==’@’||s[s.length()-1]==’#’||s[s.length()-1]==’%’||s[s.length()-1]==’&’||s[s.length()-1]==’?’)
{
cout<<“NO”<<endl;
}
else
{
for(ll i=0;i<s.length();i++)
{
if(upper_case==-1&&isupper(s[i]))
{
upper_case=0;
}
else if(lower_case==-1&&islower(s[i]))
{
lower_case=0;
}
else if(number==-1&&isdigit(s[i]))
{
number=0;
}
else if(s[i]==’@’||s[i]==’#’||s[i]==’%’||s[i]==’&’||s[i]==’?’)
{
s_c=0;
}
if(lower_case==0&&upper_case==0&&number==0&&s_c==0)
{
break;
}
}
if(lower_case==0&&upper_case==0&&number==0&&s_c==0)
{
cout<<“YES”<<endl;
}
else
{
cout<<“NO”<<endl;
}
}
}
return 0;
}

WHATS WRONG WITH MY CODE

This was the exact mistake I had made in my implementation assuming that the capital letters, symbols and digits should be STRICTLY INSIDE i.e not the first or last character. But in a later implementation, I realized that regardless of whatever the string contains, we just need to check the following conditions in ONE PASS over the entire string:

1) if(islower(s[i])  //if there is at least one lowercase letter ANYWHERE in string s
2) else if(isupper(s[i]) && (i>0 && i<n-1)) //if there is atleast one uppercase character in the middle of string s, i.e it is not the first or last character
3) else if((s[i] == '@' || s[i] == '#' || s[i] == '%' || s[i] == '?' || s[i] == '&') && (i>0 && i<n-1)) // If there is any one of these special characters strictly inside the string
4) else if((s[i]>='0' && s[i] <='9') && (i>0 && i<n-1))  // if there are digits strictly inside the string

initialize respective boolean flags for checking the conditions.
Initialize the booleans to false, and make them true as and when the conditions meet.

at the end, check if all flags are true, then print “YES” else, print "NO"

NOTE: A string S that contains capital letters, symbols or digits in beginning or end is still valid if it satisfies the given conditions.

1 Like

got it bro thank u

1 Like

Mention not! :slight_smile:

1 Like