First N Common Characters - Spicy Coders

Recent

Friday, December 01, 2017

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
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.

Source Code://Python:

from collections import OrderedDict
n=input().strip()
v=input().strip()
m=int(input())
n=list("".join(OrderedDict.fromkeys(n)))
v=list("".join(OrderedDict.fromkeys(v)))
x=[x for x in n if x in v]
print("".join(x[i] for i in range(m))

No comments:

Post a Comment