PASSWD - Editorial

PROBLEM LINK:

Practice
Contest: Division 3

Author: Daanish Mahajan
Tester: Rahul Dugar
Editorialist: Aman Dwivedi

DIFFICULTY:

Cakewalk

PREREQUISITES:

None

PROBLEM:

Given a password in the form of a string, check if the password is secure. A secured password has the following conditions:

  • It must contain at least one lower case letter [a−z].

  • It must contain at least one upper case letter [A-Z], strictly inside it.

  • It must contain at least one digit [0-9], strictly inside it.

  • It must contain at least one special character from the given set [@, #, %, &, ?], strictly inside it.

  • It should be at least 10, characters in length.

QUICK EXPLANATION:

For the given string check whether all conditions are satisfied. For doing so, you can iterate over all the characters of the string and keep checking that.

If all the conditions are satisfied print YES, otherwise NO.

TIME COMPLEXITY:

O(N) per testcase, where N is length of string.

SOLUTIONS:

Setter
Tester
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#ifndef rd
#define trace(...)
#define endl '\n'
#endif
#define pb push_back
#define fi first
#define se second
#define int long long
typedef long long ll;
typedef long double f80;
#define double long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define sz(x) ((long long)x.size())
#define fr(a,b,c) for(int a=b; a<=c; a++)
#define rep(a,b,c) for(int a=b; a<c; a++)
#define trav(a,x) for(auto &a:x)
#define all(con) con.begin(),con.end()
const ll infl=0x3f3f3f3f3f3f3f3fLL;
const int infi=0x3f3f3f3f;
//const int mod=998244353;
const int mod=1000000007;
typedef vector<int> vi;
typedef vector<ll> vl;
 
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> oset;
auto clk=clock();
mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
int rng(int lim) {
	uniform_int_distribution<int> uid(0,lim-1);
	return uid(rang);
}
int powm(int a, int b) {
	int res=1;
	while(b) {
		if(b&1)
			res=(res*a)%mod;
		a=(a*a)%mod;
		b>>=1;
	}
	return res;
}
 
long long readInt(long long l, long long r, char endd) {
	long long x=0;
	int cnt=0;
	int fi=-1;
	bool is_neg=false;
	while(true) {
		char g=getchar();
		if(g=='-') {
			assert(fi==-1);
			is_neg=true;
			continue;
		}
		if('0'<=g&&g<='9') {
			x*=10;
			x+=g-'0';
			if(cnt==0) {
				fi=g-'0';
			}
			cnt++;
			assert(fi!=0 || cnt==1);
			assert(fi!=0 || is_neg==false);
 
			assert(!(cnt>19 || ( cnt==19 && fi>1) ));
		} else if(g==endd) {
			if(is_neg) {
				x=-x;
			}
			assert(l<=x&&x<=r);
			return x;
		} else {
			assert(false);
		}
	}
}
string readString(int l, int r, char endd) {
	string ret="";
	int cnt=0;
	while(true) {
		char g=getchar();
		assert(g!=-1);
		if(g==endd) {
			break;
		}
		cnt++;
		ret+=g;
	}
	assert(l<=cnt&&cnt<=r);
	return ret;
}
long long readIntSp(long long l, long long r) {
	return readInt(l,r,' ');
}
long long readIntLn(long long l, long long r) {
	return readInt(l,r,'\n');
}
string readStringLn(int l, int r) {
	return readString(l,r,'\n');
}
string readStringSp(int l, int r) {
	return readString(l,r,' ');
}
 
 
int yes[256];
int sum_n=0;
void solve() {
	string s=readStringLn(1,20);
	sum_n+=sz(s);
	assert(sum_n<=1000'000);
	for(char i:s)
		assert(yes[i]);
	int ans=0;
	for(char i:s)
		if(yes[i]==1)
			ans=1;
	for(int i=1; i+1<sz(s); i++)
		ans|=yes[s[i]];
	if(ans==15&&sz(s)>=10) {
		cout<<"YES"<<endl;
	} else
		cout<<"NO"<<endl;
}
 
signed main() {
	fr(i,'a','z')
		yes[i]=1;
	fr(i,'A','Z')
		yes[i]=2;
	fr(i,'0','9')
		yes[i]=4;
	for(char i:{'@','#','%','&','?'})
		yes[i]=8;
	ios_base::sync_with_stdio(0),cin.tie(0);
	srand(chrono::high_resolution_clock::now().time_since_epoch().count());
	cout<<fixed<<setprecision(10);
	int t=readIntLn(1,1000000);
//	int t;
//	cin>>t;
	fr(i,1,t)
		solve();
#ifdef rd
	cerr<<endl<<endl<<endl<<"Time Elapsed: "<<((double)(clock()-clk))/CLOCKS_PER_SEC<<endl;
#endif
}
Editorialist
#include<bits/stdc++.h>
using namespace std;
 
void solve(){
  string s; cin>>s;
 
  bool len=false,small=false,large=false,digit=false,spec=false;
 
  int n=(int)s.size();
  if(n>=10) len=true;
 
  for(int i=0;i<n;i++){
    if(s[i]>='a' && s[i]<='z') small=true;
    if(i!=0 && i!=n-1){
      if(s[i]>='A' && s[i]<='Z') large=true;
      if(s[i]>='0' && s[i]<='9') digit=true;
      if(s[i]=='@' || s[i]=='#' || s[i]=='%' || s[i]=='&' || s[i]=='?') spec=true;
    }
  }
 
  if(len && small && large && digit && spec){
    cout<<"YES"<<"\n";
  }
  else{
    cout<<"NO"<<"\n";
  }
}
 
int main(){
  ios_base::sync_with_stdio(0);
  cin.tie(0);
 
  int t; cin>>t;
  while(t--){
    solve();
  }
 
return 0;
}
 

VIDEO EDITORIAL:

1 Like

is your solution passing the 2nd testcase which was provided in the question?

10 Likes
#cookOff#P1

this testcase passes all condition what is wrong in this case?

2 Likes

in sample test case
U@code4CHEFINA
output:YES
but output should be NO
because condition no 2 is not satisfied…
condition no 2:->Password must contain at least one upper case letter [A−Z][A−Z] strictly inside, i.e. not as the first or the last character;

10 Likes

Can Anyone tell me why this is not working ?

#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstring>
#include <chrono>
#include <complex>
#define endl "\n"
#define ll long long int
#define vi vector<int>
#define vll vector<ll>
#define vvi vector < vi >
#define pii pair<int,int>
#define pll pair<long long, long long>
#define mod 1000000007
#define inf 1000000000000000001;
#define all(c) c.begin(),c.end()
#define mp(x,y) make_pair(x,y)
#define mem(a,val) memset(a,val,sizeof(a))
#define eb emplace_back
#define f first
#define s second

using namespace std;
void check(string a){
  bool upper=false;
  bool lower=false;
  bool digit=false;
  bool special=false;
  string specialcharacter={'@', '#', '%', '&', '?'};
  for (size_t i = 0; i < a.length(); i++) {
    if(i==0 || i==a.length()-1){
      if(islower(a[i])){
        lower=true;
      }
    }
    else{
      if(isupper(a[i])){
        upper=true;
      }
      else if(islower(a[i])){
        lower=true;
      }
      else if(isdigit(a[i])){
        digit=true;
      }
      else{
        bool found = (std::find(specialcharacter.begin(), specialcharacter.end(), a[i]) != specialcharacter.end());
        if(found){
        special=true;}
     }
    }
  }
  // cout<<upper<<endl;
  // cout<<lower<<endl;
  // cout<<digit<<endl;
  // cout<<special<<endl;
  if(upper==true && lower==true && special==true && digit==true){
    cout<<"YES"<<endl;

  }
  else{
    cout<<"NO"<<endl;

  }
}
int main()
{
	std::ios::sync_with_stdio(false);
	int T;
	cin>>T;
	// cin.ignore(); must be there when using getline(cin, s)
	while(T--)
	{
    string a;
    cin>>a;
    if(a.length()>=10){
      check(a);

    }
    else{

      cout<<"N0"<<endl;
    }


	}
	return 0;
}

having the same doubt

**this will be the correct ans 4 this ques acc. to its prerequisites **

#include
#include <conio.h>
using namespace std;
int main(int argc, char const *argv[])

{
int t;

cin>>t;

while(t--){

    int L=0,U=0,S=0,N=0;

    string s;

    cout<<"String: ";

    cin>>s;

    int n=s.length()-1;

    bool ans=false;

    if(s.length()>=10 && !(s[0]=='@' || s[0]=='&' || s[0]=='?' || s[0]=='#' || s[0]=='%' || isdigit(s[0]) || isupper(s[0])) && !(s[n]=='@' || s[n]=='&' || s[n]=='?' || s[n]=='#' || s[n]=='%' || isdigit(s[n]) || isupper(s[n])))

    {

        for(int i=0;i<s.length();i++)

        {

            if(islower(s[i]))

                L=1;

            if(i!=0 && i!=s.length()-1)

            {

                if(isupper(s[i]))

                U=1;

                if(isdigit(s[i]))

                N=1;

                if(s[i]=='@' || s[i]=='&' || s[i]=='?' || s[i]=='#' || s[i]=='%')

                S=1;

            }    

        }

    if((L==1) && (U==1) && (S==1) && (N==1))

        ans=true;

    }

    if(ans==true)

        cout<<"YES\n";

    else

        cout<<"NO\n";

}

getch();

return 0;

}

1 Like

Yes, same here

Could someone help me with my code. I’m having some trouble with it.

#include<bits/stdc++.h>
using namespace std;

int t,x,n,i=0,j,flag,ans;
bool counta, countA, countsp, countnum;

int main(){
string s;
countnum=countsp=countA=counta=false;

cin>>t;
while(t--){
    cin>>s;
    if(s.length()>=10){
        if(islower(s[i])) counta=true;
            for(i=1; i<s.length()-1; i++){
                if(islower(s[i])) counta=true;
                 if(isupper(s[i])) countA=true;
                 if(isdigit(s[i])) countnum=true;
                 if(s[i]=='#' || s[i]=='&'|| s[i]=='%'|| s[i]=='@'|| s[i]=='?')countsp=true; 
            }
        if(counta && countA&& countnum&& countsp) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }else cout<<"NO"<<endl;
 
}
return 0;

}

In the qs it is mentioned that

Password must contain at least one digit [0−9]strictly inside;

and in the given test case, there are no numbers strictly inside , hence the output should be “NO”

The output is okay. In the qs it is mentioned that

Password must contain at least one upper case letter [A−Z]strictly inside, i.e. not as the first or the last character;

It means apart from the first and last char, the password must contain at least one uppercase letter inside, and in the given test case U@code4CHEFINA we have upper case letters inside.

Similarly, the output for #AAAab%b11cfgf will also be YES as there is at least one special char strictly inside, apart from the first and last char.

I might be wrong, but this is what I concluded after reading the problem statement carefully.

2 Likes

My solution passes all 3 testcases given in the example but I am getting WA.
My solution is viewsolution/41886187.
My solution is similar to editorialist’s so I copied it and tried in practise section and I got WA.
Can someone please check if something is wrong or it is my mistake.

The output should be ALL CAPS.

1 Like

Thank you. Very stupid from me :frowning:

1 Like

You can compare your solution with the solution which was submitted successfully and got an AC.

Great!!

#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t–)
{
string str;
getline(cin,str);
int p=str.size();
if(p>=10)
{
for(int i=0;i<p;i++)
{
if(islower(str[i]))
{
if(i>0 && i<p-1)
{
if(isupper(str[i]))
{
if(isdigit(str[i]))
{
if(str[i]==’@’ || str[i]==’%’|| str[i]==’#’ || str[i]==’&’ ||str[i]==’?’)
{
cout<<“YES”<<endl;
}
}
}
}
}
}
}
else{
cout<<“NO”<<endl;
}
}
return 0;
}

Why the code not giving correct output please help anyone