Pattern Matching

16.18 Pattern Matching: You are given two strings, pattern and value. The pattern string consists of just the letters a and b, describing a pattern within a string. For example, the string cat catgoc at go matches the pattern aabab (where cat is a and go is b). It also matches patterns like a, ab, and b. Write a method to determine if value matches pattern.

  • Brute Force:

    • Generate all possible substrings of the value. This will generate n^2 different 'words'. Then, with these these words, pair it up with all the substrings. This will generate n^4 pairs. Now using each pair of substrings, pass it into a function that use these pairs as the two distinguishing strings, and determine whether or not it matches the sequence pattern.

  • Optimized:

    • There are some basic cases we can eliminate:

      • If the value is empty or if the pattern exceeds the value, is it always false.

      • If the pattern is 1 char, then we know that it can represent any 'word', so set to true

    • With the remainder of the cases, one thing we can do is generate the number of valid combinations. This is best demonstrated with an example.

  • If we're given pattern = aabaa and value=catcatgocatcat

  • Then are are only so many patterns (number of characters) that we can define to a and b, just that they make out the full sequence of the string.

A = freq(a) = 4
B = freq(b) = 1
l = length = 14

a = number of letters assigned to a
b = number of letters assigned to b

Aa + Bb = l
b = (l - Aa) / B
4a + b = 14
  • Now we have an infinity number of solutions that are defined by this number line. We can constrain it in the following ways:

    • Valid solutions must be in the top left quadrant of the cartisian plane, since we need a and b to both be positive.

    • Both a and b must exist at least once so ab0a \neq b \neq 0 because we cannot have zero characters.

    • a must be defined from 1azero(b)1 \leq a \leq zero(b) and b defined 1bzero(a)1 \leq b \leq zero(a) because of the two statements above.

    • Both a and b must be discrete values (we cannot have half a character). To test for discrete values, we must check for whole numbers from 1azero(b)1 \leq a \leq zero(b) .

  • On this number line, we are given 3 possibilities:

    • (1,10), (2,6), (3,2)

  • Testing our solution:

    • (1,10) implies that a = value.at(0) = c, then a = value.at(1) = a, so invalid.

    • (2,6) == a = ca; tc; so invalud

    • and finally (3,2); cat ; cat; cat; go; cat; cat (valid)

Last updated

Was this helpful?