First N Repeated Characters

The program must accept a string S and then print all the characters which are among the first N repeated characters in S.

Boundary Condition(s):
1 <= Length of S <= 1000
1 <= N <= 100

Input Format:
The first line contains S.
The second line contains N.

Output Format:
The first line contains the string output.

Example Input/Output 1:
Input:
abcdeabecdacdbe
4

Output:
abceabecacbe

Explanation:
Though all characters a, b, c, d, e are repeated thrice, the first four repeated characters are a b c e.

Example Input/Output 2:
Input:
officialwork
2

Output:
ffii

a=input().strip()
b=int(input())
l,l1=[],[]
for i in range(len(a)):
    for j in range(i+1,len(a)):
        if a[i]==a[j]:
            l.append(a[i])
            l1.append(j)
            break
l1.sort()
l=[]
for i in range(b):
    l.append(a[l1[i]])
l.sort()

for i in range(len(a)):
    if a[i] in l :
        print(a[i],end="")

Leave a Reply

Your email address will not be published. Required fields are marked *

More posts. You may also be interested in.