First N Repeated Characters

 Coding Problem Keys 

 First N Repeated Characters 

 Problem Statement 

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

 Input Format 

The first line contains the value of S.
The second line contains the value of N.

 Output Format 

The first line contains the string output.

 Constraints 

1 <= Length of S
 <= 1000
1 <= N <= 100

 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

 Max Execution Time Limit: 500 millisecs 

 Solution 

 Programming Language: Python 3 Language 

def firstRepeatedChar(s,n):
    h = {}
    k=[]
    for ch in s:
        if ch in h and len(set(k))<n:
            k.append(ch);
        else:
            h[ch] = 0
    for i in s:
        if i in k:
            print(i,end="")
s=input().strip()
n=int(input())
firstRepeatedChar(s,n)

# Published By PKJCODERS

 (Note: Incase If the code doesn't Pass the output kindly comment us with your feedback to help us improvise.) 

Comments

Popular Posts

Maximum Sub Square Sum

Search String S2 in S1 Character Matrix

Largest of Tenth Digit and Unit Digit

ASCII Based Encryption

String With Most Vowels

Reverse Column - First and Last