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 ; function to find the minimum index character ; unordered_map ' um ' implemented as hash table ; 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 program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printMinIndexChar ( string str , string patt ) { unordered_map < char , int > um ; int minIndex = INT_MAX ; int m = str . size ( ) ; int n = patt . size ( ) ; - for ( int i = 0 ; i < m ; i ++ ) if ( um . find ( str [ i ] ) == um . end ( ) ) um [ str [ i ] ] = i ; for ( int i = 0 ; i < n ; i ++ ) if ( um . find ( patt [ i ] ) != um . end ( ) && um [ patt [ i ] ] < minIndex ) minIndex = um [ patt [ i ] ] ; if ( minIndex != INT_MAX ) cout << " Minimum ▁ Index ▁ Character ▁ = ▁ " << str [ minIndex ] ; else cout << " No ▁ character ▁ present " ; } int main ( ) { string str = " geeksforgeeks " ; string patt = " set " ; printMinIndexChar ( str , patt ) ; return 0 ; }