-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathminimum-indexed-character.cpp
61 lines (51 loc) · 1.21 KB
/
minimum-indexed-character.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// code written by whynespower
// question link https://practice.geeksforgeeks.org/problems/minimum-indexed-character-1587115620/1#
// { Driver Code Starts
//Initial template for C++
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
//User function template for C++
class Solution
{
public:
//Function to find the minimum indexed character.
int minIndexChar(string str, string patt)
{
unordered_map<char, long long>ss;
for(long long i=0; i<str.length(); i++){
if(ss.find(str[i])==ss.end()){
ss[str[i]]=i;
}
}
long long ans=INT_MAX;
for(long long i=0; i<patt.length(); i++){
auto it= ss.find(patt[i]);
if( it !=ss.end()){
ans=min(ans, it->second);
}
}
if(ans==INT_MAX){
return -1;
}
else return ans;
// Your code here
}
};
// { Driver Code Starts.
int main()
{
int t;
cin>>t;
while(t--)
{
string str;
string patt;
cin>>str;
cin>>patt;
Solution obj;
cout<<obj.minIndexChar(str, patt)<<endl;
}
return 0;
}
// } Driver Code Ends