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.
We have existing solution for this problem, please refer Check if string follows order of characters defined by a pattern or not | Set 1. Here we solve this problem quickly in python using OrderedDict(). Approach is very simple,
- Create an OrderedDict of input string which contains characters of input strings as Key only.
- Now set a pointer at the start of pattern string.
- Now traverse generated OrderedDict and match keys with individual character of pattern string, if key and character matches with each other then increment pointer by 1.
- If pointer of pattern reaches it’s end that means string follows order of characters defined by a pattern otherwise not.
This article is contributed by Shashank Mishra (Gullu). 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.
- K’th Non-repeating Character in Python using List Comprehension and OrderedDict
- LRU Cache in Python using OrderedDict
- OrderedDict in Python
- Python - Insertion at the beginning in OrderedDict
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Python - Check whether a string starts and ends with the same character or not (using Regular Expression)
- Find a string such that every character is lexicographically greater than its immediate next character
- Replace every character of string by character whose ASCII value is K times more than it
- Replace every character of a string by a different character
- Map every character of one string to another such that all occurrences are mapped to the same character
- Modify the string such that every character gets replaced with the next character in the keyboard
- Replace all occurrences of character X with character Y in given string
- Longest Common Prefix using Character by Character Matching
- Python | Sorting string using order defined by another string
- Python | Insert character after every character pair
- Python program to read character by character from a file
- Check if a two character string can be made using given words
- Count substrings that starts with character X and ends with character Y
- Shortest distance to every other character from given character
- Nearest smaller character to a character K from a Sorted Array