Find the character in first string that is present at minimum index in second string | Method to find the minimum index character ; map to store the first index of each character of ' str ' ; To store the index of character having minimum index ; Lengths of the two strings ; Store the first index of each character of ' str ' ; Traverse the string ' patt ' ; If patt [ i ] is found in ' um ' , check if it has the minimum index or not accordingly update ' minIndex ' ; Print the minimum index character ; If no character of ' patt ' is present in ' str ' ; Driver Code
function printMinIndexChar ( str , patt ) { let hm = new Map ( ) ; let minIndex = Number . MAX_VALUE ; let m = str . length ; let n = patt . length ; for ( let i = 0 ; i < m ; i ++ ) if ( ! hm . has ( str [ i ] ) ) hm . set ( str [ i ] , i ) ; for ( let i = 0 ; i < n ; i ++ ) if ( hm . has ( patt [ i ] ) && hm . get ( patt [ i ] ) < minIndex ) minIndex = hm . get ( patt [ i ] ) ; if ( minIndex != Number . MAX_VALUE ) document . write ( " " + str [ minIndex ] ) ; else document . write ( " " ) ; } let str = " " ; let patt = " " ; printMinIndexChar ( str , patt ) ;