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 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.
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.
- Most frequent word in first String which is not present in second String
- Remove characters from the first string which are present in the 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
- Count of substrings having the most frequent character in the string as first character
- Given two strings, find if first string is a subsequence of second
- Program to find second most frequent character
- Check whether the Average Character of the String is present or not
- 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
- Check whether second string can be formed from characters of first string
- Find just strictly greater element from first array for each element in second array
- First string from the given array whose reverse is also present in the same array
- Find i'th Index character in a binary string obtained after n iterations
- Find last index of a character in a string