Given an input string and a pattern, check if characters in the input string follows the same order as determined by characters present in the pattern. Assume there won’t be any duplicate characters in the pattern.
Input: string = "engineers rock" pattern = "er"; Output: true Explanation: All 'e' in the input string are before all 'r'. Input: string = "engineers rock" pattern = "egr"; Output: false Explanation: There are two 'e' after 'g' in the input string. Input: string = "engineers rock" pattern = "gsr"; Output: false Explanation: There are one 'r' before 's' in the input string.
The idea is very simple. For every pair (x, y) of consecutive characters in the pattern string, we find the last occurrence of x and first occurrence of y in the input string. If last occurrence of character x is after first occurrence of character y for any pair, we return false. Checking for every pair of consecutive characters in the pattern string will suffice. For example, if we consider three consecutive characters in the pattern say x, y and z, if (x, y) and (y, z) returns true, that implies (x, z) is also true.
Below is the implementation of above idea –
We have discussed two more approaches to solve this problem.
Check if string follows order of characters defined by a pattern or not | Set 2
Check if string follows order of characters defined by a pattern or not | Set 3
This article is contributed by Aditya Goel. 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
- Check if string follows order of characters defined by a pattern or not | Set 2
- Check if string follows order of characters defined by a pattern or not | Set 3
- Check if the characters of a given string are in alphabetical order
- Sort a string according to the order defined by another string
- Check whether two strings contain same characters in same order
- Check if lowercase and uppercase characters are in same order
- Python | Get the smallest window in a string containing all characters of given pattern
- Print all distinct characters of a string in order (3 Methods)
- Check if a string follows a^nb^n pattern or not
- Check whether second string can be formed from characters of first string
- Check if both halves of the string have same set of characters
- Quick way to check if all the characters of a string are same
- Check whether the vowels in a string are in alphabetical order or not
- Check whether the frequencies of all the characters in a string are prime or not
- Check if a given string is made up of two alternating characters