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 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:
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 email@example.com. 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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Remove characters from the first string which are present in the second string
- Most frequent word in first String which is not present in second String
- Find elements which are present in first array and not in second
- Find repeated character present first in a string
- Count elements present in first array but not in second
- Elements present in first array and not in second using STL in C++
- Frequency of smallest character in first sentence less than that of second sentence
- Minimum changes required to make first string substring of second string
- Find the largest Alphabetic character present in the string
- Given two strings, find if first string is a subsequence of second
- Check whether the Average Character of the String is present or not
- Program to find second most frequent character
- Check whether second string can be formed from characters of first string
- Count subsequences in first string which are anagrams of the second string
- Count of times second string can be formed from the characters of first string
- First string from the given array whose reverse is also present in the same array
- Find just strictly greater element from first array for each element in second array
- Find last index of a character in a string
- Find i'th Index character in a binary string obtained after n iterations
- Queries to find the first non-repeating character in the sub-string of a string