Check if string follows order of characters defined by a pattern or not | Set 2
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.
Examples:
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.
Illustration:
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 Java implementation of above steps.
// Java program to check if characters of a string follow // pattern defined by given pattern. import java.util.*; public class OrderOfCharactersForPattern { public static boolean followsPattern(String str, String pattern) { // Insert all characters of pattern in a hash set, Set<Character> patternSet = neHashSet<>(); for (int i=0; i<pattern.length(); i++) patternSet.add(pattern.charAt(i)); // Build modified string (string with characters only from // pattern are taken) StringBuilder modifiedString = new StringBuilder(str); for (int i=str.length()-1; i>=0; i--) if (!patternSet.contains(modifiedString.charAt(i))) modifiedString.deleteCharAt(i); // Remove more than one consecutive occurrences of pattern // characters from modified string. for (int i=modifiedString.length()-1; i>0; i--) if (modifiedString.charAt(i) == modifiedString.charAt(i-1)) modifiedString.deleteCharAt(i); // After above modifications, the length of modified string // must be same as pattern length if (pattern.length() != modifiedString.length()) return false; // And pattern characters must also be same as modified string // characters for (int i=0; i<pattern.length(); i++) if (pattern.charAt(i) != modifiedString.charAt(i)) return false; return true; } // Driver program int main() { String str = "engineers rock"; String pattern = "er"; System.out.println("Expected: true, Actual: " + followsPattern(str, pattern)); str = "engineers rock"; pattern = "egr"; System.out.println("Expected: false, Actual: " + followsPattern(str, pattern)); str = "engineers rock"; pattern = "gsr"; System.out.println("Expected: false, Actual: " + followsPattern(str, pattern)); str = "engineers rock"; pattern = "eger"; System.out.println("Expected: true, Actual: " + followsPattern(str, pattern)); return 0; } } |
Output:
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 contribute@geeksforgeeks.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.
Recommended Posts:
- Return maximum occurring character in an input string
- Print all the duplicates in the input string
- Remove characters from the first string which are present in the second string
- Remove duplicates from a given string
- A Program to check if strings are rotations of each other or not
- Print reverse of a string using recursion
- Write a program to print all permutations of a given string
- Divide a string in N equal parts
- Given a string, find its first non-repeating character
- Write a program to reverse an array or string
- Print list items containing all characters of a given word
- Check for balanced parentheses in an expression
- Reverse words in a given string
- Find the smallest window in a string containing all characters of another string
- Naive algorithm for Pattern Searching



