Posts Minimum indexed character (geeksforgeeks - SDE Sheet)
Post
Cancel

Minimum indexed character (geeksforgeeks - SDE Sheet)

PROBLEM DESCRIPTION

Given a string str and another string patt. Find the minimum index of the character in str that is also present in patt.

geeksforgeeks

SOLUTION

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution
{
    //Function to find the minimum indexed character.
    public static int minIndexChar(String str, String patt)
    {
        Set<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;
    }
}
This post is licensed under CC BY 4.0 by the author.