text: Lexicographically smallest string with given string as prefix | Function to find the whether the string temp starts with str or not ; Base Case ; Check for the corresponding characters in temp & str ; Function to find lexicographic smallest string consisting of the string str as prefix ; Sort the given array string arr [ ] ; If the i - th string contains given string as a prefix , then print the result ; If no string exists then return " - 1" ; Driver Code
code: function is_prefix ( temp , str ) { if ( temp . length < str . length ) return 0 ; else { for ( let i = 0 ; i < str . length ; i ++ ) { if ( str [ i ] != temp [ i ] ) return 0 ; } return 1 ; } } function lexicographicallyString ( input , n , str ) { input = Array . from ( input ) . sort ( ) ; for ( let i = 0 ; i < n ; i ++ ) { let temp = input [ i ] ; if ( is_prefix ( temp , str ) ) { return temp ; } } return " " ; } let arr = [ " " , " " , " " , " " , " " ] ; let S = " " ; let N = 5 ; document . write ( lexicographicallyString ( arr , N , S ) ) ;