236A - Boy or Girl In this problem we have to find out if the number of distinct characters in one's user name is odd or even. How to count the unique characters in a string in c++? TIA.
# | User | Rating |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
# | User | Contrib. |
---|---|---|
1 | cry | 166 |
2 | maomao90 | 163 |
2 | Um_nik | 163 |
4 | atcoder_official | 161 |
5 | adamant | 160 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | nor | 153 |
9 | Dominater069 | 153 |
236A - Boy or Girl In this problem we have to find out if the number of distinct characters in one's user name is odd or even. How to count the unique characters in a string in c++? TIA.
Name |
---|
Sort the vector (call this vector v)
Then use v.erase(unique(v.begin(), v.end()), v.end());
Then your answer is just the size of the vector afterwards.
Read about erase and unique to understand what they do.
Alternatively you could insert all the elements in a set and return it's size
If you are interested only in the count of unique characters of a string
x
, it suffices to do the following:can you describe it please?
Check this out. (sorting is necessary to get all similar characters in a continuous segment)
I have understood the sorting. But i can't understood this: unique(x.begin(), x.end()) — x.begin();
Unique return iterator to last character. Unique — begin would give the index of last character which is also the number of unique characters in the string.
Anyway I think using a bucket is better than sorting and unique() since we only have lowercase Latin letters.
One-liner if you're really lazy:
set(s.begin(), s.end()).size()