Problem Statement:
Given a string str and another string patt. Find the character in patt that is present at the minimum index in str. If no character of patt is present in str then print ‘$’.
Input:
The first line of input contains an integer T denoting the number of test cases. Then the description of T test cases follow. Each test case contains two strings str and patt respectively.
Output:
Output the character in patt that is present at the minimum index in str. Print "$" (without quotes) if no character of patt is present in str.
User Task:
The task is to complete the function printMinIndexChar() which finds the character in patt that is present at minimum index in str.
Constraints:
1 <= T <= 105
1 <= |str|, |patt| <= 105
Example:
Input:
2
geeksforgeeks
set
adcffaet
onkl
Output:
e
$
Explanation:
Testcase 1: e is the character which is present in given patt "geeksforgeeks" and is first found in str "set".
import java.io.*;
import java.util.*;
class GFG
Input:
The first line of input contains an integer T denoting the number of test cases. Then the description of T test cases follow. Each test case contains two strings str and patt respectively.
Output:
Output the character in patt that is present at the minimum index in str. Print "$" (without quotes) if no character of patt is present in str.
User Task:
The task is to complete the function printMinIndexChar() which finds the character in patt that is present at minimum index in str.
Constraints:
1 <= T <= 105
1 <= |str|, |patt| <= 105
Example:
Input:
2
geeksforgeeks
set
adcffaet
onkl
Output:
e
$
Explanation:
Testcase 1: e is the character which is present in given patt "geeksforgeeks" and is first found in str "set".
Solution:
import java.io.*;
import java.util.*;
class GFG
{
public static void main (String[] args) throws IOException
public static void main (String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
while(t-- > 0)
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
while(t-- > 0)
{
String str = br.readLine();
String patt = br.readLine();
PrintString obj = new PrintString();
System.out.println(obj.printMinIndexChar(str, patt));
}
}
}
class PrintString
String str = br.readLine();
String patt = br.readLine();
PrintString obj = new PrintString();
System.out.println(obj.printMinIndexChar(str, patt));
}
}
}
class PrintString
{
public static String printMinIndexChar(String str, String patt)
{
HashMap<Character,Integer> h = new HashMap();
for(int i=0; i<str.length(); i++)
{
if(!h.containsKey(str.charAt(i)))
h.put(str.charAt(i) , i);
}
String min = "$";
int minIndex = 10000;
for(int i=0; i<patt.length(); i++)
{
if(h.containsKey(patt.charAt(i)))
if(h.get(patt.charAt(i)) < minIndex)
{
minIndex = h.get(patt.charAt(i));
min = patt.charAt(i) + "";
}
}
return min;
}
}
public static String printMinIndexChar(String str, String patt)
{
HashMap<Character,Integer> h = new HashMap();
for(int i=0; i<str.length(); i++)
{
if(!h.containsKey(str.charAt(i)))
h.put(str.charAt(i) , i);
}
String min = "$";
int minIndex = 10000;
for(int i=0; i<patt.length(); i++)
{
if(h.containsKey(patt.charAt(i)))
if(h.get(patt.charAt(i)) < minIndex)
{
minIndex = h.get(patt.charAt(i));
min = patt.charAt(i) + "";
}
}
return min;
}
}
No comments:
Post a Comment