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 ; to store the index of character having minimum index ; lengths of the two strings ; traverse ' patt ' ; for each character of ' patt ' traverse ' str ' ; if patt [ i ] is found in ' str ' , check if it has the minimum index or not . If yes , then update ' minIndex ' and break ; 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 ) { int minIndex = INT_MAX ; int m = str . size ( ) ; int n = patt . size ( ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( patt [ i ] == str [ j ] && j < minIndex ) { minIndex = j ; break ; } } } 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 ; }