Help Needed! Guys

This code is for the problem PASSWD .

Can anybody tell me why my code is wrong,I am suspecting that the mistake I made is I allowed the first and the last character to be a character from A to Z, but the statement allowed so, that is there was no restriction on the first or the last letter being an uppercase letter.

#include <bits/stdc++.h>
#define ll long long
using namespace std;
#define INF INT_MAX
#define MAXN 100005
#define debug(x) std::cerr<<x<<endl;
#define FAST ios_base::sync_with_stdio(false);
//for fastening the programme
// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")
// #pragma GCC optimization("unroll-loops")
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void solve(){
  string s;cin>>s;
  int n=s.length();
  if(n<10){
    cout<<"NO"<<endl;
    return;
  }else{
    char symbols[]={ '@', '#', '%', '&', '?'};
    if((s[0]>='a' && s[0]<='z') || (s[0]>='A' && s[0]<='Z')){
      if((s[n-1]<='z' && s[n-1]>='a') || (s[n-1]<='Z' && s[n-1]>='A')){
        bool ok=false;
        for(int i=0;i<5;i++){
          for(int j=1;j<=n-2;j++){
            if(s[j] ==  symbols[i]){
              ok=true;
              break;
            }
          }
          if(ok)
             break;
        }
        if(!ok){
          cout<<"NO"<<endl;
          return;
        }else{
          ok=false;
          for(int i=1;i<=n-2;i++){
            if( (s[i]>='0') && (s[i]<='9')){
              ok=true;
              break;
            }
          }
          if(!ok){
            cout<<"NO"<<endl;
            return;
          }else{
                           ok=false;
                           for(int i=1;i<=n-2;i++){
                            if(s[i] >='A' && s[i]<='Z'){
                              ok=true;
                              break;
                            }
                           }
                           if(!ok){
                            cout<<"NO"<<endl; // some "A" or "B" inside the string.
                            return;
                           }else{
                            ok=false;
                            for(int i=0;i<n;i++){
                              if(s[i]>='a' && s[i]<='z'){
                                ok=true;
                                cout<<"YES"<<endl;
                                return;
                              }
                            }
                            cout<<"NO"<<endl;
                            return;

                           }
          }
        }

      }else{
        cout<<"NO"<<endl;
        return;
      }
    }else{
      cout<<"NO"<<endl;
      return;
    }
  }
}
int main() {
  FAST;
//   #ifndef ONLINE_JUDGE
//     freopen("input.txt","r",stdin);
//     freopen("output.txt","w",stdout);
//   #endif
    int tt;cin>>tt;
    while(tt--){
      solve();
    

    }
    
    
    
     
}

Why does the first and last character of the string need to be from a-z or A-Z?

it’s mentioned in the question?
the first and the last can’t be digits and the special symbols.
That’s it.

Let’s slow down and reflect on that a bit.

Virat Kohli: I will make at least 1 century between 2020 and 2021
Implication: Virat Kohli will not make any centuries before 2020 or after 2021.

You see what’s wrong?

1 Like

Sorry, i still don’t get you.
It would be very helpful, if you code point out some mistake in the code, then it would be very easy for me to correct it.
Thanks.

Mistake is not in the code. You have coded what you have understood.
The mistake is in your understanding. Please read the question again once.

The question says: At least 1 capital letter in the string excluding first and last characters. It means only that. It does not mean no capital letter in first and last characters. For example, a string has 10 characters. It means that the password must have at least one capital letter from the 2nd till the 9th character. It does not mean that password cannot additionally have 1st or last character as a capital letter .
Example:
AbcdefgHiJ has the capital letter condition valid because H is a capital letter between 1st and last character.

The same logic also goes for special characters and digits.

Generally speaking, in the field of logic, let us look at 3 statements:

  1. there exists some x such that condition C is true
  2. There exists no x such that condition C is false
  3. For all x, condition C is true

If you are given the statement 1, it does not imply 2 (as you are trying to imply)
If you are given 3 however, then 2 is implied.

So in this case, if you are given that ALL capital letters must occur between 1st and last character, then what you did would be correct. But you are not given that. You are only given that at least 1 capital letter is between 1st and last character.

Please don’t hesitate to ask if it’s still not clear

1 Like

Okay, i get it now ig.
The first and the last can be anyting, even the special characters and numbers, we just need to make sure, that each occurs inside in the string.

Thanks for the explanation!.