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 All 'e' in the input string are before all 'r'. Input: string = "engineers rock", pattern = "egr"; Output: false There are two 'e' after 'g' in the input string. Input: string = "engineers rock", pattern = "gsr"; Output: false There are one 'r' before 's' in the input string.
We have discussed two approaches to solve this problem.
Check if string follows order of characters defined by a pattern or not | Set 1
Check if string follows order of characters defined by a pattern or not | Set 2
In this approach we first assign a label (or order) to characters of pattern. The labels are assigned in increasing order.
For example, the pattern “gsr” is labeled as following
"g" => 1 "s" => 2 "r" => 3
It means ‘g’ will come first, then ‘s’, then ‘r’
After assigning labels to pattern characters, we iterate through string characters. While traversing, we keep track of label (or order) of last visited character. If label of current character is less than previous character, we return false. Otherwise we update last label. If all characters follow order, we return true.
Below is the implementation
Time Complexity of this program is O(n) with constant extra space (the array label is of constant size, 256).
This article is contributed by Mohammed Iftekharul Islam Rupam. 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 1
- Check if string follows order of characters defined by a pattern or not | Set 2
- 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 if a string is made up of K alternating characters
- Check whether the frequencies of all the characters in a string are prime or not