Smallest substring occurring only once in a given string | 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 ; Return the minimum of dictionary ; Driver Code
function smallestSubstring ( a ) { let a1 = [ ] ; for ( let i = 0 ; i < a . length ; i ++ ) { for ( let j = i + 1 ; j <= a . length ; j ++ ) { if ( i != j ) a1 . push ( a . substring ( i , j ) ) ; } } let a2 = new Map ( ) ; for ( let s = 0 ; s < a1 . length ; s ++ ) { if ( a2 . has ( a1 [ s ] ) ) a2 . set ( a1 [ s ] , a2 . get ( a1 [ s ] ) + 1 ) ; else a2 . set ( a1 [ s ] , 1 ) ; } let freshlist = [ ] ; for ( let s of a2 . keys ( ) ) { if ( a2 . get ( s ) == 1 ) freshlist . push ( s ) ; } let dictionary = new Map ( ) ; for ( let s = 0 ; s < freshlist . length ; s ++ ) { dictionary . set ( freshlist [ s ] , freshlist [ s ] . length ) ; } let newlist = [ ] ; for ( let s of dictionary . keys ( ) ) newlist . push ( dictionary . get ( s ) ) ; let ans = Number . MAX_VALUE ; for ( let i = 0 ; i < newlist . length ; i ++ ) ans = Math . min ( ans , newlist [ i ] ) ; return ans == Number . MAX_VALUE ? 0 : ans ; } let S = " " ; document . write ( smallestSubstring ( S ) ) ;