0

I created an algorithm to check matching strings given that some characters might be a regular expression defined by '*', or '.' I'm trying to analyze its complexity; however I can't see what the Big O for this recursive function really is.

The algorithm takes 2 strings as input, and returns a boolean if one string can be matched by the second or vice-versa.

Example:

A = "abc"
B = "ab*"
Output = true

A = "ab."
B = "abc"
Output = true

A = "abcd*"
B = "ab"
Output = false



public static boolean match(String a, String b) {
        //Check if a == b , if so, strings match
        if (a.equals(b))
            return true;
        //Check if second char in a in an asterisk,  return true if substring of b matches b.
        if (hasAsterisk(a) && match(a.substring(2), b))
            return true;
        //Check if second char of b is an asterisk, return true if substring of b matches a.
        if (hasAsterisk(b) && match(a, b.substring(2)))
            return true;
        //Check if a and b is not empty
        if (!a.isEmpty() && !b.isEmpty()) {
            //Check if first char of a is asterisk, if so match a substring with b
            if (a.charAt(0) == '*')
                return match(a.substring(1), b);
            //Check if first char of b is asterisk, if so match b substring with a
            if (b.charAt(0) == '*')
                return match(a, b.substring(1));
            //Check if first char of a or b is "DOT(.)", if so match substrings.
            return charMatch(a.charAt(0), b.charAt(0)) 
                    && match(a.substring(1), b.substring(1));
        }
        //If not match, return false
        else 
            return false;

    }

    private static boolean hasAsterisk(String a) {
        return a.length() > 1 && a.charAt(1) == '*';
    }

    private static boolean charMatch(char a, char b) {
        return a == b || a == '.' || b == '.';
    }

My follow up question is, how can this run more efficiently?

Thank you!

xDG
  • 309
  • 6
  • 18
  • possible duplicate of [Is there an equivalent of java.util.regex for "glob" type patterns?](http://stackoverflow.com/questions/1247772/is-there-an-equivalent-of-java-util-regex-for-glob-type-patterns) Also, check out this meta thread: http://meta.stackoverflow.com/questions/261841/can-i-post-questions-about-optimizing-code-on-stack-overflow – default locale Jan 16 '15 at 04:28
  • No its not a possible duplicate. Im looking for the complexity of the algorithm and a reasonable explanation of its complexity. Not looking for the code like in the post you mentioned. Please remove the "possible duplicate for this title" – xDG Jan 16 '15 at 14:43
  • I retracted my vote to close this question. I'm glad you got your answer. Anyway, please check meta discussion on questions about algorithm optimization. – default locale Jan 16 '15 at 16:19

1 Answers1

1

If you after Big O complexity then it is O(n), that's because each of your loops (in eqauls(), match() etc...) increases the number of comparisons it performs as the number of chars in your string increase. For simplicity I'll assume that both input strings are roughly the same length and hence it does't matter whether n is the length of the first or second string.

If you are looking to improve your big O complexity, the bad news is you cant. If you think about it, you will realize that in the worst case (strings matching till the very last char) it is impossible to perform these comparison with out at least comparing each character once. Hence O(n).

What you can improve is the actual number of comparisons by not repeating the same comparison more than once. There is plenty of the that in your code.

hhafez
  • 38,949
  • 39
  • 113
  • 143