B - Perfect String Editorial by en_translator
Use a loop structure like a for statement to check if all conditions are satisfied.
Check if it contains an uppercase letter by inspecting all characters in a total of \(\mathrm{O}(|S|)\) time.
Similarly, check if it contains an lowercase letter by inspecting all characters in a total of \(\mathrm{O}(|S|)\) time.
Check if \(S_i \neq S_j\) for every pair of integers \((i, j)\) such that \(1 \le i < j \le |S|\) in a total of \(\mathrm{O}(|S|^2)\) time.
In C++, one can check if a character is an uppercase English letter by ‘A’ <= s[i] && s[i] <= ‘Z’
or by isupper function.
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
bool big = false,small = false;
for(int i = 0; i < s.size(); i++){
if(isupper(s[i])) big = true;
else small = true;
}
bool diff = true;
for(int i = 0; i < s.size(); i++){
for(int j = i + 1; j < s.size(); j++){
if(s[i] == s[j]) diff = false;
}
}
if(big && small && diff) cout << "Yes" << endl;
else cout << "No" << endl;
}
posted:
last update: