-
Notifications
You must be signed in to change notification settings - Fork 4
/
commonChar.cpp
58 lines (46 loc) · 1.37 KB
/
commonChar.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
// Given two strings s1 and s2 consisting of lowercase English alphabets,
// the task is to count all the pairs of indices (i, j)
// from the given strings such that s1[i] = s2[j] and all the indices are distinct i.e. if s1[i]
// pairs with some s2[j] then these two characters will not be paired with any other character.
// Example program
#include <iostream>
#include <string>
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s1;
string s2;
cin>>s1; cin>>s2;
set <char> str1;
set <char> str2;
set <char> :: iterator itr1,itr2;
// set <char> :: iterator itr2;
for(int i=0;i<s1.length();i++){
str1.insert(s1[i]);
}
for(int i=0;i<s2.length();i++){
str2.insert(s2[i]);
}
for (itr1 = str1.begin(); itr1 != str1.end(); ++itr1)
{
cout << *itr1<<" ";
}
cout<<endl;
for (itr2 = str2.begin(); itr2 != str2.end(); ++itr2)
{
cout << *itr2<<" ";
}
cout<<endl;
int count=0;
for(itr1 = str1.begin(); itr1 != str1.end(); ++itr1)
{
for(itr2 = str2.begin(); itr2 != str2.end(); ++itr2){
if( *itr1 == *itr2)
{ cout<<*itr1<< " "<<*itr2<<endl;
count++; }
}
}
cout<<endl;
cout<<"output : "<<count;
}