logo CodeStepByStep logo

matchPattern

Language/Type: JavaScript algorithms interview strings

Write a function named matchPattern that accepts two strings as parameters; the first representing a "pattern" and the second representing a string to check against that pattern. Your function should return true if there is a match between the pattern and the string. A match in this case is defined as a valid substitution where each occurrence of a symbol in the pattern could be mapped to a particular word in the string.

So, for example, if the pattern is "abba" and the string to search is "hi bye bye hi", you should return true because a could represent "hi" and b could represent "bye". If the string to search were "hi bye hi bye", you would return false because in this case the string's pattern is "abab", not "abba".

You may assume that the pattern and string to search will be non-empty, and that words are separated by exactly one space.

Function: Write a JavaScript function as described, not a complete program.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.