I encountered this problem in an interview and I was stuck on the best way to go about it. The question is as follows: 

Given a string sequence of words and a string sequence pattern, return true if the sequence of words matches the pattern otherwise false.

Definition of match: A word that is substituted for a variable must always follow that substitution. For example, if "f" is substituted as "monkey" then any time we see another "f" then it must match "monkey" and any time we see "monkey" again it must match "f".

**Examples**  

input: "ant dog cat dog", "a d c d"  

output: true  

This is true because every variable maps to exactly one word and vice verse.  

a -> ant  

d -> dog  

c -> cat  

d -> dog  

input: "ant dog cat dog", "a d c e"  

output: false  

This is false because if we substitute "d" as "dog" then you can not also have "e" be substituted as "dog".  

a -> ant  

d, e -> dog (Both d and e can't both map to dog so false)  

c -> cat  

input: "monkey dog eel eel", "e f c c"  

output: true  

This is true because every variable maps to exactly one word and vice verse.  

e -> monkey  

f -> dog  

c -> eel  

Initially, I thought of doing something as follows...

```
function matchPattern(pattern, stringToMatch) {
  var patternBits = pattern.split(" ");
  var stringBits = stringToMatch.split(" ");
  var dict = {};

  if (patternBits.length < 0 
      || patternBits.length !== stringBits.length) {
    return false;
  }

  for (var i = 0; i < patternBits.length; i++) {
    if (dict.hasOwnProperty(patternBits[i])) {
      if (dict[patternBits[i]] !== stringBits[i]) {
        return false;
      }
    } else {
      dict[patternBits[i]] = stringBits[i];
    }
  }
  return true;
}

var ifMatches = matchPattern("a e c d", "ant dog cat dog");
console.log("Pattern: " + (ifMatches ? "matches!" : "does not match!"));

```

However, I realized that this won't work and fails example #2 as it erroneously returns true. One way to deal with this issue is to use a bi-directional dictionary or two dictionaries i.e store both {"a": "ant"} and
{"ant": "a"} and check both scenarios in the if check. However, that seemed like wasted space. Is there a better way to tackle this problem without using regular expressions?