-
Notifications
You must be signed in to change notification settings - Fork 4
/
Different Names.cpp
64 lines (59 loc) · 1.63 KB
/
Different Names.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
/*
PROBLEM:
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
Sample Input 2:
Abhishek Harshit Ayush Iti
Sample Output:
-1
CODE:
*/
#include<bits/stdc++.h>
using namespace std;
int main (){
string s;
int count=0;
// Taking input string from user
getline(cin,s);
// Taking an empty string temp
string temp="";
// Defining map
unordered_map<string,int> m;
for(int i=0;i<s.length();i++){
if(s[i]==' '){
// Iterating the names into map
m[temp]++;
temp="";
}else{
temp=temp+s[i];
}
if(i==s.length()-1){
m[temp]++;
}
}
// Declaring the iterator to map
unordered_map<string,int>::iterator it;
for(it=m.begin();it!=m.end();it++){
if(it->second>=2){
// Printing the repeated names along with their frequency
cout<<it->first<<" "<<it->second<<endl;
count++;
}
}
if(count==0)
cout<<-1;
return 0;
}