forked from jinghao-jia/Hackerrank_Practice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTest.cpp
63 lines (50 loc) · 1.6 KB
/
Test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <string>
#include <list>
#include <map>
using namespace std;
//1. white space
//2. duplicates
//3. Capital >> Lowercase
//4. Sort
int main() {
string line;
while (getline(cin, line)) {
list<char> charList; // list to hold the characters
map<char, int> charMap; // Char map to keep track on the occurence
// To traverse the string and put the characters into the list
for (int i = 0; i < line.size(); i++) {
char character = line[i];
// If character is not space
if(character != ' ') {
//If it is a capital letter, change to lower case
if(character <= 'Z' && character >= 'A') {
character = character - ('A' - 'a');
}
//If haven't seen the char, add it to the map
//Else, skip it
if(charMap.find(character) == charMap.end()) {
charMap[character] = 1;
}
else {
continue;
}
//push Characters in to the list
charList.push_back(character);
}
}
//Sort the list
charList.sort();
//Reset the string
line = "";
//Push those characters back to the string and commas
for(list<char>::iterator it = charList.begin(); it != charList.end();
it++) {
line += *it;
line += ',';
}
//Erase the last comma
line.erase(line.size() - 1);
cout << line << endl;
}
}