Pramp Interview: Basic Regex Parser #111
Replies: 5 comments
-
Make sure your peer is familiar with regular expressions, and if not, explain the concept briefly with more examples. |
Beta Was this translation helpful? Give feedback.
-
The solution for this kind of pattern matching is usually done by recursion. Review first the algorithm below and then read the explanation that follows. |
Beta Was this translation helpful? Give feedback.
-
`Pseudocode:function isMatch(text, pattern): Input:text - the text to check,pattern - the regular expression to be checked,textIndex - the index the text is checked frompatIndex - the index the pattern is checked fromOutput:true if the text from the index textIndex matchesthe regular expression pattern from the pattern Index.E.g. isMatchHelper(“aaabb”,”cab*”,2, 1) since the textfrom index 2 (“abb”) matches the pattern from index 1 (“ab*”)function isMatchHelper(text, pattern, textIndex, patIndex):
|
Beta Was this translation helpful? Give feedback.
-
Note: in the code, a text of length N begins in position 0 and ends in position N-1. As we can see, this is a recursive function that has many cases to cover: First the base cases - if textIndex points to the end of the text: If the pattern also points to the end of the text, then both are the empty string and both match. If the pattern[patIndex] is '.', or the (pattern[patIndex] == text[textIndex]) we proceed to check the next indexes, as the current indexes match. If the current characters match or the pattern in the current index is '.' then: The 'x*' in the pattern (where x is the current character in the pattern) is matched as a sequence of zero characters - in which case we simply increment patIndex by 2. Space Complexity: the space required is also exponential because of the number of recursion calls filling up the stack. There are, in fact, algorithms that solve the matching problem in polynomial time and space. They are based on Nondeterministic Finite State Machines, which we didn’t provide here due to the fact that it requires more knowledge in theoretical computer science. |
Beta Was this translation helpful? Give feedback.
-
Basic Regex Parser
Implement a regular expression function isMatch that supports the '.' and '' symbols. The function receives two strings - text and pattern - and should return true if the text matches the pattern as a regular expression. For simplicity, assume that the actual symbols '.' and '' do not appear in the text string and are used as special symbols only in the pattern string.
In case you aren’t familiar with regular expressions, the function determines if the text and pattern are the equal, where the '.' is treated as a single a character wildcard (see third example), and '*' is matched for a zero or more sequence of the previous letter (see fourth and fifth examples). For more information on regular expression matching, see the Regular Expression Wikipedia page.
Explain your algorithm, and analyze its time and space complexities.
`input: text = "aa", pattern = "a"
output: false
input: text = "aa", pattern = "aa"
output: true
input: text = "abc", pattern = "a.c"
output: true
input: text = "abbb", pattern = "ab*"
output: true
input: text = "acd", pattern = "ab*c."
output: true`
Constraints:
[time limit] 5000ms
[input] string text
[input] string pattern
[output] boolean
Beta Was this translation helpful? Give feedback.
All reactions