-
Notifications
You must be signed in to change notification settings - Fork 1
/
minimumIndexCharacter.java
46 lines (36 loc) · 1.13 KB
/
minimumIndexCharacter.java
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
// { Driver Code Starts
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine().trim());
while(T-->0)
{
String str = br.readLine().trim();
String patt = br.readLine().trim();
Solution obj = new Solution();
System.out.println(obj.printMinIndexChar(str, patt));
}
}
}// } Driver Code Ends
class Solution{
// Function to find the character in patt which is present in str at min index
public static String printMinIndexChar(String str, String patt){
HashSet<Character> set = new HashSet<>();
for(int i=0;i<patt.length();i++){
set.add(patt.charAt(i));
}
for(int i=0;i<str.length();i++){
char ch = str.charAt(i);
if(set.contains(ch)){
String s = Character.toString(ch);
return s;
}
}
return "$";
}
}