Given two string, in which one is pattern (Pattern) and other is searching expression. Searching expression contains ‘#’.
The # works in following way:
- A # matches with one or more characters.
- A # matches all characters before a pattern match is found. For example if pat = “A#B”, and text is “ACCBB”, then # would match only with “CC” and pattern is considered as not found.
Input : str = "ABABABA" pat = "A#B#A" Output : yes Input : str = "ABCCB" pat = "A#B" Output : yes Input : str = "ABCABCCE" pat = "A#C#" Output : yes Input : str = "ABCABCCE" pat = "A#C" Output : no
We can observe that whenever we encounter ‘#’, we have to consider as many characters till the next character of pattern will not be equal to the current character of given string. Firstly, we check if the current character of pattern is equal to ‘#’-
a) If not then we check whether the current character of string and pattern are same or not, if same, then increment both counters else return false from here only. No need for further checking.
b) If yes, then we have to find the position of a character in text that matches with next character of pattern.
This article is contributed by Roshni Agarwal. 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.
- Naive algorithm for Pattern Searching
- KMP Algorithm for Pattern Searching
- Rabin-Karp Algorithm for Pattern Searching
- Optimized Naive Algorithm for Pattern Searching
- Pattern Searching | Set 6 (Efficient Construction of Finite Automata)
- Finite Automata algorithm for Pattern Searching
- Boyer Moore Algorithm for Pattern Searching
- Pattern Searching using Suffix Tree
- String matching where one string contains wildcard characters
- Pattern Searching using a Trie of all Suffixes
- Z algorithm (Linear time pattern searching Algorithm)
- Search a Word in a 2D Grid of characters
- Aho-Corasick Algorithm for Pattern Searching
- Wildcard Pattern Matching
- Find all the patterns of "1(0+)1" in a given string | SET 2(Regular Expression Approach)
Improved By : nitin mittal