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.
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.
- 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 a string follows a^nb^n pattern or not
- Check if given Binary string follows then given condition or not
- Sort a string according to the order defined by another string
- Check if the characters of a given string are in alphabetical order
- Minimize steps defined by a string required to reach the destination from a given source
- Check whether the vowels in a string are in alphabetical order or not
- Check whether count of distinct characters in a string is Prime or not
- Check whether the frequencies of all the characters in a string are prime or not
- Python | Get the smallest window in a string containing all characters of given pattern
- Check if lowercase and uppercase characters are in same order
- Check whether two strings contain same characters in same order
- Print all distinct characters of a string in order (3 Methods)
- Minimize Cost to sort a String in Increasing Order of Frequencies of Characters
- Minimum characters required to be removed to sort binary string in ascending order
- Generate a Number in Decreasing order of Frequencies of characters of a given String
- Largest string obtained in Dictionary order after deleting K characters
- Min flips of continuous characters to make all characters same in a string
- String with k distinct characters and no same characters adjacent