Saturday, June 3, 2017

Smallest Substring - All Distinct Letters

A string value S containing only alphabets (both lower and uppercase) is passed as input to the program. The program must print the size of the smallest sub-string which contains all the distinct alphabets in the string S. (lowercase and uppercase characters are treated as two different unique characters)

Input Format:

First line contains S.

Output Format:

First line contains the size of the smallest substring which contains all the distinct characters in the string S.

Example Input/Output 1:

Input:
abcdfffccbbfeaad

Output:
8

Explanation:
The smallest sub string that contains all the distinct letters is cbbfeaad whose length is 8. 

Solution:

s = input()
st = set(s)
a = [0]*len(s)
for i in range(len(s)):
    for j in range (1,len(s)+1):
        x = s[i:j]
        xt = set(x)
        if len(st) == len(xt):
            if len(x)<len(a):
                a = x

print(a)                
print(len(a))

Share: