Minimum indexed character | Practice | GeeksforGeeks

Given a string str and another string patt. Find the first position (considering 0-based indexing) of the character in patt that is present at the minimum index in str.

Example 1:
Input:
str = geeksforgeeks
patt = set
Output: 1
Explanation: e is the character which is
present in given str "geeksforgeeks"
and is first found in patt "set". First Position
of e in str is 1. 

Example 2:
Input:
str = adcffaet
patt = onkl
Output: -1
Explanation: There are none of the
characters which is common in patt
and str.

Your Task:
You only need to complete the function minIndexChar() that returns the index of answer in str or returns -1 in case no character of patt is present in str.

Expected Time Complexity: O(N).
Expected Auxiliary Space: O(Number of distinct characters).

Constraints:
1 ≤ |str|,|patt| ≤ 105 
'a' <= stri, patti <= 'z'
0
tanashah8 minutes ago
 unordered_map<char,int>mp;
        for(int i=0;i<patt.size();i++){
            mp[patt[i]]==1;
        }
        for(int i=0;i<str.size();i++){
            if(mp.find(str[i])!=mp.end()){
                return i;
            }
        }
        return -1;//TC O(m+n)
0
rohanyadav49834 days ago
Time Limit Exceeded, Can any1 suggest for optimization?
 //Function to find the minimum indexed character.   public static int minIndexChar(String str, String patt)   {       char x[]=str.toCharArray();       char y[]=patt.toCharArray();             int a=0;       for(int i=0;i<x.length;i++){           for(int j=0;j<y.length;j++){               if(x[i]==y[j]){                                      a=i;                   return a;                                                     }                                  }           }              return -1;   }}
0
lindan1235 days ago
int minIndexChar(string str, string patt)   {              unordered_map<int,int> mp;       int n = str.length();       for(int i=0;i<n;i++)       {           if(mp.find(str[i])==mp.end())           {               mp[str[i]]=i;           }       }              int maxx=INT_MAX;       int m = patt.length();       for(int i=0;i<m;i++)       {           if(mp.find(patt[i])!=mp.end())           {               if(maxx>mp[patt[i]])               {                   maxx=mp[patt[i]];               }           }       }              if(maxx==INT_MAX)       {           return -1;       }       return maxx;   }
 
Time Taken : 0.4sec
Cpp
0
jayesh296 days ago
Java 0.6 sec
    public static int minIndexChar(String str, String patt){
        HashMap<Character,Integer> hm = new HashMap<>();
        for(int i=0;i<str.length();i++){
            if(!hm.containsKey(str.charAt(i))){
                hm.put(str.charAt(i),i);
            }
        }
        int res = Integer.MAX_VALUE;
        for(int i=0;i<patt.length();i++){
            if(hm.containsKey(patt.charAt(i))){
                res = Math.min(res,hm.get(patt.charAt(i)));
            }
        }
        if(res==Integer.MAX_VALUE)  return -1;
        else    return res;
    }
}
0
abhishekvicky123452 weeks ago
er function template for JAVA
class Solution{   //Function to find the minimum indexed character.   public static int minIndexChar(String str, String patt)   {       HashMap<Character,Integer> ary=new HashMap<>();              for(int i=0;i<str.length();i++)       {           if(!ary.containsKey(str.charAt(i)))           {               ary.put(str.charAt(i),i);           }       }              int min=100001;       int flag=0;       for(int i=0;i<patt.length();i++)       {       if(ary.containsKey(patt.charAt(i)))       {           if(ary.get(patt.charAt(i))<min)           {               min=ary.get(patt.charAt(i));               flag=1;            }        }        }        if(flag==1)        return min;        return -1;}}
0
abera25831 month ago
class Solution{ public:   //Function to find the minimum indexed character.   int minIndexChar(string str, string patt)   {       // Your code here       set<char>s;       for(auto value:patt){           s.insert(value);       }       for(int i=0;i<str.size();i++){           if(s.find(str[i])!=s.end()){               return i;           }          }       return -1;   }};
0
vrajeshmodi991 month ago
int minIndexChar(string str, string patt)
    {
        unordered_map<char,int>mp;
        for(int i=0;i<patt.size();i++){
            mp[patt[i]]==1;
        }
        for(int i=0;i<str.size();i++){
            if(mp.find(str[i])!=mp.end()){
                return i;
            }
        }
        return -1;
    }
0
singhdipranjan671 month ago
 public static int minIndexChar(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++)
        {
            if(set.contains(str.charAt(i)))
            return i;
        }
       return -1; 
    }
}

0
imabhishek021 month ago
Simple cpp program we just counted the each character. like if any character is present in patt then ++ that charcter after that we just compared str characters is present in patt it would have already increased by so if that character count >0 then we just print ith of character of str string;
int minIndexChar(string str, string patt)
    {
        int fr[26]={0};
        
        for(int i=0;i<patt.size();i++)
        {
            char ch=patt[i];
            fr[ch-'a']++;
        }
         for(int i=0;i<str.size();i++)
        {
            char ch=str[i];
            
            if(fr[ch-'a']>0)
            {
                return i;
            }
        
        }return -1;
 
0
roopsaisurampudi1 month ago
HashMap<Character, Integer> hm = new HashMap<>();       int len1 = str.length();       int len2 = patt.length();       for(int i = 0; i < len1; i++) {           if (hm.containsKey(str.charAt(i))); //skip           else hm.put(str.charAt(i), i);       }       int minimum = Integer.MAX_VALUE;       for(int i = 0; i < len2; i++) {           if (hm.containsKey(patt.charAt(i))) minimum = Math.min(minimum, hm.get(patt.charAt(i)));
       }       return minimum == Integer.MAX_VALUE ? -1: minimum;
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.