Find the character in first string that is present at minimum index in second string | C # implementation to 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
using System ; using System . Collections . Generic ; class GFG { static void printMinIndexChar ( String str , String patt ) { Dictionary < char , int > hm = new Dictionary < char , int > ( ) ; int minIndex = int . MaxValue ; int m = str . Length ; int n = patt . Length ; for ( int i = 0 ; i < m ; i ++ ) if ( ! hm . ContainsKey ( str [ i ] ) ) hm . Add ( str [ i ] , i ) ; for ( int i = 0 ; i < n ; i ++ ) if ( hm . ContainsKey ( patt [ i ] ) && hm [ patt [ i ] ] < minIndex ) minIndex = hm [ patt [ i ] ] ; if ( minIndex != int . MaxValue ) Console . WriteLine ( " Minimum ▁ Index ▁ Character ▁ = ▁ " + str [ minIndex ] ) ; else Console . WriteLine ( " No ▁ character ▁ present " ) ; } public static void Main ( String [ ] args ) { String str = " geeksforgeeks " ; String patt = " set " ; printMinIndexChar ( str , patt ) ; } }