A friend of mine just had his interview at Google and got rejected because he couldn't give a solution to this question.
I have my own interview in a couple of days and can't seem to figure out a way to solve it.
Here's the question:
You are given a pattern, such as [a b a b]. You are also given a string, example "redblueredblue". I need to write a program that tells whether the string follows the given pattern or not.
A few examples:
Pattern: [a b b a] String: catdogdogcat returns 1
Pattern: [a b a b] String: redblueredblue returns 1
Pattern: [a b b a] String: redblueredblue returns 0
I thought of a few approaches, like getting the number of unique characters in the pattern and then finding that many unique substrings of the string then comparing with the pattern using a hashmap. However, that turns out to be a problem if the substring of a is a part of b.
It'd be really great if any of you could help me out with it. :)
UPDATE:
Added Info: There can be any number of characters in the pattern (a-z). Two characters won't represent the same substring. Also, a character can't represent an empty string.
You can use the Pattern. matches() method to quickly check if a text (String) matches a given regular expression. Or you can compile a Pattern instance using Pattern. compile() which can be used multiple times to match the regular expression against multiple texts.
The function regex_search() is used to search for a pattern in the string that matches the regular expression. Consider the following C++ program that shows the usage of regex_search().
The easiest way to check if a Python string contains a substring is to use the in operator. The in operator is used to check data structures for membership in Python. It returns a Boolean (either True or False ).
The simplest solution that I can think of is to divide the given string into four parts and compare the individual parts. You don't know how long a
or b
is, but both a
s are of the same length as well as b
s are. So the number of ways how to divide the given string is not very large.
Example:
pattern = [a b a b]
, given string = redblueredblue
(14 characters in total)
|a|
(length of a
) = 1, then that makes 2 characters for a
s and 12 characters is left for b
s, i.e. |b|
= 6. Divided string = r edblue r edblue
. Whoa, this matches right away!|a| = 2, |b| = 5
-> divided string = re dblue re dblue
-> matchExample 2:
pattern = [a b a b]
, string = redbluebluered
(14 characters in total)
|a| = 1, |b| = 6
-> divided string = r edblue b luered
-> no match|a| = 2, |b| = 5
-> divided string = re dblue bl uered
-> no match|a| = 3, |b| = 4
-> divided string = red blue blu ered
-> no matchThe rest is not needed to be checked because if you switched a
for b
and vice versa, the situation is identical.
What is the pattern that has [a b c a b c] ?
Don't you just need to translate the pattern to a regexp using backreferences, i.e. something like this (Python 3 with the "re" module loaded):
>>> print(re.match('(.+)(.+)\\2\\1', 'catdogdogcat'))
<_sre.SRE_Match object; span=(0, 12), match='catdogdogcat'>
>>> print(re.match('(.+)(.+)\\1\\2', 'redblueredblue'))
<_sre.SRE_Match object; span=(0, 14), match='redblueredblue'>
>>> print(re.match('(.+)(.+)\\2\\1', 'redblueredblue'))
None
The regexp looks pretty trivial to generate. If you need to support more than 9 backrefs, you can use named groups - see the Python regexp docs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With