logo Practice-It logo

isMatch

Language/Type: Java method basics return Strings
Author: Marty Stepp (on 2020/04/16)

Write a static method called isMatch that takes a pattern string and a target string as parameters and that returns whether or not the given target matches the pattern. Patterns can contain special wildcard characters dot (".") and star ("*"). If a pattern does not contain any wildcards, then the target has to be the same string, as in isMatch("and", "and"). A dot can match any single character. For example, the pattern "a.." matches any 3-letter string beginning with the letter "a". A star can match any sequence of characters (including no characters). For example, the pattern "a*t" matches any string that begins with "a" and ends with "t", including "at". There will be at most one star in any given pattern, although a pattern can contain several dots and a star. Below are examples of patterns and matching strings (note that your method compares a pattern against a single string, not a list of strings).

Pattern Matching strings
"hello" hello
"..." and, ant, but, cat, cow, hat, sat, tap, ten, the, tot
"a..." atom, army, aunt, aura
".a.." bats, task, yard, saga, lava
"...a" tuna, soda, coma, aura, saga, lava
"....th" growth, zenith, health
"a*" a, an, at, and, ant, atom, aunt, apple, army, aura
"t*t" tot, that, trot, tiniest
"the*" the, then, there, therefore, thermal, thespians
".a*a" saga, lava, saliva, tarantula, nausea
"t.e.p" twerp, trespass, thespians
".o*e." poem, token, wolves, voucher, toothbrushes

You are allowed to create new strings, but otherwise you are not allowed to construct extra data structures to solve this problem (no array, ArrayList, Scanner, etc). You are limited to the string methods covered in class (otherwise this problem can be solved in one line of code).

Type your solution here:


This is a method problem. Write a Java method as described. Do not write a complete program or class; just the method(s) above.

You must log in before you can solve this problem.


Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.