Traverse str2 and str1 , and compare current character of str2 with first unmatched char of str1 , if matched then move ahead in str1
for ( $ i = 0 ; $ i < $ n && $ j < $ m ; $ i ++ ) if ( $ str1 [ $ j ] == $ str2 [ $ i ] ) $ j ++ ;