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’.
Input: str = “geeksforgeeks”, patt = “set”
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”.
Minimum Index Character = e
Time Complexity: O(mn), where m and n are the length 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++.
Below image is a dry run of the above approach:
Below is the implementation of the above approach:
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.
This article is contributed by Ayush Jauhari. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Find repeated character present first in a string
- Find the largest Alphabetic character present in the string
- Find last index of a character in a string
- Find i'th Index character in a binary string obtained after n iterations
- Find the longest sub-string which is prefix, suffix and also present inside the string | Set 2
- Find the longest sub-string which is prefix, suffix and also present inside the string
- Check whether the Average Character of the String is present or not
- Queries to find the last non-repeating character in the sub-string of a given string
- Queries to find the first non-repeating character in the sub-string of a string
- Largest index for each distinct character in given string with frequency K
- Index of character depending on frequency count in string
- Find a string such that every character is lexicographically greater than its immediate next character
- Minimum cost to traverse from one index to another in the String
- Minimum sum of squares of character counts in a given string after removing k characters
- Minimum string such that every adjacent character of given string is still adjacent