Official

A - Distinct Strings Editorial by en_translator


We can divide into the following cases:

  • If the three characters in \(S\) are all the same, then the answer is clearly \(1\).
  • Otherwise, if the three characters in \(S\) are all different, then the answer is \(3!=6\).
  • If neither the two cases above apply, then \(S\) consists of two same characters and another character other than them, hence the answer is \(3\).

One can get AC by directly implementing them.

Sample code (C++)

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

int main(){
    string S; cin >> S;
    int ans = 3;
    if(S[0]==S[1] && S[1]==S[2]) ans = 1;
    else if(S[0]!=S[1] && S[1]!=S[2] && S[2]!=S[0]) ans = 6;
    cout << ans << endl;
}

Sample code (Python)

S = input()
ans = 3
if S[0] == S[1] and S[1] == S[2]:
    ans = 1
elif S[0] != S[1] and S[1] != S[2] and S[0] != S[2]:
    ans = 6
print(ans)

One can also solve it by exhaustively searching over all permutation, where case division is not required.

Sample code (C++)

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

int main(){
    string S; cin >> S;
    sort(S.begin(),S.end());
    set<string> s;
    do{
        s.insert(S);
    }while(next_permutation(S.begin(),S.end()));
    cout << s.size() << endl;
}

posted:
last update: