-
Notifications
You must be signed in to change notification settings - Fork 1
/
differentNames.cpp
75 lines (56 loc) · 1.68 KB
/
differentNames.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
64
65
66
67
68
69
70
71
72
73
74
// Different Names
// In Little Flowers Public School, there are many students with same first names. You are given a task to find the students with same names. You will be given a string comprising of all the names of students and you have to tell the name and count of those students having same. If all the names are unique, print -1 instead.
// Note: We don't have to mention names whose frequency is 1.
// Input Format:
// The only line of input will have a string ‘str’ with space separated first names of students.
// Output Format:
// Print the names of students along with their count if they are repeating. If no name is repeating, print -1
// Constraints:
// 1 <= |str| <= 10^5
// Time Limit: 1 second
// Sample Input 1:
// Abhishek harshit Ayush harshit Ayush Iti Deepak Ayush Iti
// Sample Output 1:
// harshit 2
// Ayush 3
// Iti 2
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s;
getline(cin, s);
vector<string> names;
string name = "";
for(int i = 0; i < s.length(); i++)
{
if(s[i] == ' ')
{
names.push_back(name);
name = "";
continue;
}
name = name + s[i];
}
names.push_back(name);
map<string, int> freq;
for(int i = 0; i < names.size(); i++)
{
freq[names[i]]++;
}
bool unique = true;
map<string, int> :: iterator itr;
for(itr = freq.begin(); itr != freq.end(); itr++)
{
if(itr->second != 1)
{
unique = false;
cout << itr->first << " " << itr->second << endl;
}
}
if(unique)
{
cout << -1 << endl;
}
return 0;
}