Q: Alphabetical Strings CF 1547B #deque in C++

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

void solve(){
    string s;
    cin >> s;

    deque<char> dq;
    char maxi;

    for(int i=0;i<s.length();i++){
        dq.push_back(s[i]);
        maxi = max(maxi, s[i]);
    }

    // NO: if dq is empty; if no letter to be compare

    for(char ch = maxi;ch >= 'a'; ch--){
        if(dq.size() == 0) cout << "NO" << endl;
        if(dq.front() == ch) dq.pop_front();
        else if(dq.back() == ch) dq.pop_back();
        else cout << "NO" << endl;
    }

    if(dq.size() == 0) cout << "YES" << endl;
    else cout << "NO" << endl;
}

int main(){
    int t;
    cin >> t;
    while(t--){
        solve();
    }
    return 0;
}

Comments

Popular Posts