Smallest substring occurring only once in a given string | Python3 program of the above approach ; Function to find the smallest substring occurring only once ; Stores all occurences ; Generate all the substrings ; Avoid multiple occurences ; Append all substrings ; Take into account all the substrings ; Iterate over all unique substrings ; If frequency is 1 ; Append into fresh list ; Initialize a dictionary ; Append the keys ; Traverse the dictionary ; Print the minimum of dictionary ; Driver Code
from collections import Counter NEW_LINE def smallestSubstring ( a ) : NEW_LINE INDENT a1 = [ ] NEW_LINE for i in range ( len ( a ) ) : NEW_LINE INDENT for j in range ( i + 1 , len ( a ) ) : NEW_LINE INDENT if i != j : NEW_LINE INDENT a1 . append ( a [ i : j + 1 ] ) NEW_LINE DEDENT DEDENT DEDENT a2 = Counter ( a1 ) NEW_LINE freshlist = [ ] NEW_LINE for i in a2 : NEW_LINE INDENT if a2 [ i ] == 1 : NEW_LINE INDENT freshlist . append ( i ) NEW_LINE DEDENT DEDENT dictionary = dict ( ) NEW_LINE for i in range ( len ( freshlist ) ) : NEW_LINE INDENT dictionary [ freshlist [ i ] ] = len ( freshlist [ i ] ) NEW_LINE DEDENT newlist = [ ] NEW_LINE for i in dictionary : NEW_LINE INDENT newlist . append ( dictionary [ i ] ) NEW_LINE DEDENT return ( min ( newlist ) ) NEW_LINE DEDENT S = " ababaabba " NEW_LINE print ( smallestSubstring ( S ) ) NEW_LINE