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.
Another solution to the same problem is posted here.
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.
The idea here is to reduce the given string to the pattern given. For characters given in the pattern, we only keep the corresponding characters in the string. In the new string, we delete continuous repeated characters. The modified string should then be equal to the pattern given. Lastly, we compare modified string to the pattern given and return true of false accordingly.
str = "bfbaeadeacc", pat = "bac" 1) Remove extra characters from str (characters that are not present in pat str = "bbaaacc" [f, e and d are removed] 3) Removed consecutive repeating occurrences of characters str = "bac" 4) Since str is same as pat, we return true
Below is implementation of above steps.
Expected: true, Actual: true Expected: false, Actual: false Expected: false, Actual: false Expected: true, Actual: true
Time Complexity: Time complexity of above implementations is actually O(mn + n^2) as we use deleteCharAt() to remove characters. We can optimize above solution to work in linear time. Instead of using deleteCharAr(), we can create an empty string and add only required characters to it.
StringBuilder is used to operate on input string. This is because StringBuilder is mutable, while String is immutable object. To create a new string takes O(n) space, so extra space is O(n).
We have discussed two more 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 3
This article is contributed by Lalit Ramesh Sirsikar. 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.
- 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 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)
- Generate a Number in Decreasing order of Frequencies of characters of a given String
- Largest string obtained in Dictionary order after deleting K characters
- Minimize Cost to sort a String in Increasing Order of Frequencies of Characters
- Check if a string follows a^nb^n pattern or not
- Check whether second string can be formed from characters of first string
- Quick way to check if all the characters of a string are same
- Check if both halves of the string have same set of characters
- Check whether the vowels in a string are in alphabetical order or not
- Check if a given string is made up of two alternating characters
- Check if characters of one string can be swapped to form other
- Program to check if first and the last characters of string are equal
- Check if both halves of the string have same set of characters in Python