/
FirstNCharacters.cpp
79 lines (57 loc) · 1.16 KB
/
FirstNCharacters.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
75
76
77
78
79
/* Question :
First N Common Characters
Two string values S1, S2 are passed as the input. The program must print first N characters present in S1 which are also present in S2.
Input Format:
The first line contains S1.
The second line contains S2.
The third line contains N.
Output Format:
The first line contains the N characters present in S1 which are also present in S2.
Boundary Conditions:
2 <= N <= 10<br />
2 <= Length of S1, S2 <= 1000
Example Input/Output 1:
Input:
abcbde
cdefghbb
3
Output:
bcd
Note:
b occurs twice in common but must be printed only once.
*/
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
string a,b;
cin >> a >> b;
int c[26]={0},d[26]={0},n;
cin >> n;
for(int i=0;i<a.length();i++)
c[a[i]-'a'] = 1;
for(int i=0;i<b.length();i++)
d[b[i]-'a'] = 1;
/*
for(int i=0;i<26;i++)
cout << c[i] << " ";
cout << endl;
for(int i=0;i<26;i++)
cout << d[i] << " ";
cout << endl;
*/
int count =0;
for(int i=0;i<a.length();i++)
{
if( count <n)
{
int temp = a[i]-'a';
if(c[temp] == d[temp])
{
count++;
c[temp]=0;
cout << a[i];
}
}
}
}