Find the character in first string that is present at minimum index in second string
Last Updated :
27 Sep, 2022
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 ‘No character present’.
Examples:
Input: str = “geeksforgeeks”, patt = “set”
Output: e
Both e and s of patt are present in str,
but e is present at minimum index, which is 1.
Input: str = “adcffaet”, patt = “onkl”
Output: No character present
Source: OLA Interview Experience | Set 12.
Naive Approach: Using two loops, find the first index of each character of patt in str. Print the character having the minimum index. If no character of patt is present in str then print “No character present”.
Implementation:
C++
#include <bits/stdc++.h>
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;
}
|
Java
public class GFG
{
static void printMinIndexChar(String str, String patt)
{
int minIndex = Integer.MAX_VALUE;
int m = str.length();
int n = patt.length();
for ( int i = 0 ; i < n; i++) {
for ( int j = 0 ; j < m; j++) {
if (patt.charAt(i)== str.charAt(j) && j < minIndex) {
minIndex = j;
break ;
}
}
}
if (minIndex != Integer.MAX_VALUE)
System.out.println( "Minimum Index Character = " +
str.charAt(minIndex));
else
System.out.println( "No character present" );
}
public static void main(String[] args)
{
String str = "geeksforgeeks" ;
String patt = "set" ;
printMinIndexChar(str, patt);
}
}
|
Python3
def printMinIndexChar( Str , patt):
minIndex = 10 * * 9
m = len ( Str )
n = len (patt)
for i in range (n):
for j in range (m):
if (patt[i] = = Str [j] and j < minIndex):
minIndex = j
break
if (minIndex ! = 10 * * 9 ):
print ( "Minimum Index Character = " , Str [minIndex])
else :
print ( "No character present" )
Str = "geeksforgeeks"
patt = "set"
printMinIndexChar( Str , patt)
|
C#
using System;
class GFG
{
static void printMinIndexChar(String str, String patt)
{
int minIndex = int .MaxValue;
int m = str.Length;
int n = patt.Length;
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 .MaxValue)
Console.WriteLine( "Minimum Index Character = " +
str[minIndex]);
else
Console.WriteLine( "No character present" );
}
public static void Main()
{
String str = "geeksforgeeks" ;
String patt = "set" ;
printMinIndexChar(str, patt);
}
}
|
Javascript
<script>
function printMinIndexChar(str,patt)
{
let minIndex = Number.MAX_VALUE;
let m = str.length;
let n = patt.length;
for (let i = 0; i < n; i++) {
for (let j = 0; j < m; j++) {
if (patt[i]== str[j] && j < minIndex) {
minIndex = j;
break ;
}
}
}
if (minIndex != Number.MAX_VALUE)
document.write( "Minimum Index Character = " +
str[minIndex]);
else
document.write( "No character present" );
}
let str = "geeksforgeeks" ;
let patt = "set" ;
printMinIndexChar(str, patt);
</script>
|
Output
Minimum Index Character = e
Time Complexity: O(mn), where m and n are the lengths of the two strings.
Auxiliary Space: O(1)
Method 2 Efficient Approach(Hashing):
- Create a hash table with (key, value) tuple represented as (character, index) tuple.
- Store the first index of each character of str in the hash table.
- Now, for each character of patt check if it is present in the hash table or not.
- If present then get its index from the hash table and update minIndex(minimum index encountered so far).
- For no matching character print “No character present”.
Hash table is implemented using unordered_set in C++.
The below image is a dry run of the above approach:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void printMinIndexChar(string str, string patt)
{
unordered_map< char , int > um;
int minIndex = INT_MAX;
int m = str.size();
int n = patt.size();
for ( int i = 0; i < m; i++) {
if (um.find(str[i]) == um.end())
um[str[i]] = i;
}
for ( int j = 0; j < n; j++) {
if (um.find(patt[j]) != um.end()
&& um[patt[j]] < minIndex)
minIndex = um[patt[j]];
}
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;
}
|
Java
import java.util.HashMap;
public class GFG
{
static void printMinIndexChar(String str, String patt)
{
HashMap<Character, Integer> hm = new HashMap<>();
int minIndex = Integer.MAX_VALUE;
int m = str.length();
int n = patt.length();
for ( int i = 0 ; i < m; i++)
if (!hm.containsKey(str.charAt(i)))
hm.put(str.charAt(i),i);
for ( int i = 0 ; i < n; i++)
if (hm.containsKey(patt.charAt(i)) &&
hm.get(patt.charAt(i)) < minIndex)
minIndex = hm.get(patt.charAt(i));
if (minIndex != Integer.MAX_VALUE)
System.out.println( "Minimum Index Character = " +
str.charAt(minIndex));
else
System.out.println( "No character present" );
}
public static void main(String[] args)
{
String str = "geeksforgeeks" ;
String patt = "set" ;
printMinIndexChar(str, patt);
}
}
|
Python3
import sys
def printMinIndexChar(st, patt):
um = {}
minIndex = sys.maxsize
m = len (st)
n = len (patt)
for i in range (m):
if (st[i] not in um):
um[st[i]] = i
for i in range (n):
if (patt[i] in um and
um[patt[i]] < minIndex):
minIndex = um[patt[i]]
if (minIndex ! = sys.maxsize):
print ( "Minimum Index Character = " ,
st[minIndex])
else :
print ( "No character present" )
if __name__ = = "__main__" :
st = "geeksforgeeks"
patt = "set"
printMinIndexChar(st, patt)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void printMinIndexChar(String str, String patt)
{
Dictionary< char ,
int > hm = new Dictionary< char ,
int >();
int minIndex = int .MaxValue;
int m = str.Length;
int n = patt.Length;
for ( int i = 0; i < m; i++)
if (!hm.ContainsKey(str[i]))
hm.Add(str[i], i);
for ( int i = 0; i < n; i++)
if (hm.ContainsKey(patt[i]) &&
hm[patt[i]] < minIndex)
minIndex = hm[patt[i]];
if (minIndex != int .MaxValue)
Console.WriteLine( "Minimum Index Character = " +
str[minIndex]);
else
Console.WriteLine( "No character present" );
}
public static void Main(String[] args)
{
String str = "geeksforgeeks" ;
String patt = "set" ;
printMinIndexChar(str, patt);
}
}
|
Javascript
<script>
function printMinIndexChar(str, patt)
{
let hm = new Map();
let minIndex = Number.MAX_VALUE;
let m = str.length;
let n = patt.length;
for (let i = 0; i < m; i++)
if (!hm.has(str[i]))
hm.set(str[i], i);
for (let i = 0; i < n; i++)
if (hm.has(patt[i]) &&
hm.get(patt[i]) < minIndex)
minIndex = hm.get(patt[i]);
if (minIndex != Number.MAX_VALUE)
document.write( "Minimum Index Character = " +
str[minIndex]);
else
document.write( "No character present" );
}
let str = "geeksforgeeks" ;
let patt = "set" ;
printMinIndexChar(str, patt);
</script>
|
Output
Minimum Index Character = e
Time Complexity: O(m + n), where m and n are the lengths of the two strings.
Auxiliary Space: O(d), where d is the size of hash table, which is the count of distinct characters in str.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...