Official

A - A Unique Letter Editorial by en_translator


This problem can be solved by dividing into cases as follows:

Sample Code (C++):

#include<bits/stdc++.h>

using namespace std;

int main(){
  string s;
  cin >> s;
  if(s[0]!=s[1] && s[0]!=s[2]){cout << s[0] << "\n";}
  else if(s[1]!=s[0] && s[1]!=s[2]){cout << s[1] << "\n";}
  else if(s[2]!=s[0] && s[2]!=s[1]){cout << s[2] << "\n";}
  else{cout << "-1\n";}
  return 0;
}

But, let’s be more ambitious in this editorial: can we find “how many times each character occurs in string \(S\)?”

Desired output for input \(S=\) abcbz

a : 1
b : 2
c : 1
d : 0
...
y : 0
z : 1

Basic idea is packet sort, but this time we need to use letters as indices.

There are many ways to do so. One way is to use an associative array map<char,int> (that takes a character (char type) as an index and can be handled like an array of int type; in case of map<char, int>, the values are initialized with 0.) as follows:

Sample code (C++):

#include<bits/stdc++.h>

using namespace std;

int main(){
  string s;
  cin >> s;
  map<char,int> mp;
  for(auto &nx : s){mp[nx]++;}
  for(char i='a';i<='z';i++){
    cout << i << " : " << mp[i] << "\n";
  }
  return 0;
}

We can utilize this code to solve this problem.

Sample code (C++):

#include<bits/stdc++.h>

using namespace std;

int main(){
  string s;
  cin >> s;
  map<char,int> mp;
  for(auto &nx : s){mp[nx]++;}
  for(char i='a';i<='z';i++){
    if(mp[i]==1){cout << i << "\n";return 0;}
  }
  cout << "-1\n";
  return 0;
}

posted:
last update: