views:

179

answers:

2

The following function accepts 2 strings, the 2nd (not 1st) possibly containing *'s (asterisks). An * is a replacement for a string (empty, 1 char or more), it can appear appear (only in s2) once, twice, more or not at all, it cannot be adjacent to another * (ab**c), no need to check that.

public static boolean samePattern(String s1, String s2)

It returns true if strings are of the same pattern. It must be recursive, not use any loops, static or global variables. Also it's prohibited to use the method equals in the String class. Can use local variables and method overloading. Can use only these methods: charAt(i), substring(i), substring(i, j), length().

Examples:

1: TheExamIsEasy; 2: "The*xamIs*y" ---> true
1: TheExamIsEasy; 2: "Th*mIsEasy*" ---> true
1: TheExamIsEasy; 2: "*" ---> true
1: TheExamIsEasy; 2: "TheExamIsEasy" ---> true
1: TheExamIsEasy; 2: "The*IsHard" ---> FALSE

I am stucked on this question for many hours now! I need the solution in Java please kindly help me.

+4  A: 

Looks like you might want regular expressions.

The .+ regex pattern is equivalent to your *.

But then you'd have two problems.

Eric
+1 for the classic joke
Andrei Fierbinteanu
What is .+? I don't know this symbol
Within a regular expression, `.+` means "one or more (`+`) of any character (`.`)".
Eric
Since the OP's * can be empty, the proper regex would actually be [code].*[/code]Annayena - Don't trouble yourself too much with regular expressions - you've been asked to solve the problem a particular way, so solve it that way. You might want to suggest to your tutor that regular expressions would have provided an effective alternate method of solving this problem without recursion - but learn about Regular Expressions first so you can justify your argument. There are plenty of tutorials on the internet...
Martin Milan
+2  A: 

The following is a recursive, no loop solution to your problem in Java:

static boolean samePattern(String s1, String s2) {
    return
        s2.isEmpty() ?
            s1.isEmpty()
            :
        s2.charAt(0) == '*' ?
            samePattern(s1, s2.substring(1))
            || (!s1.isEmpty() && samePattern(s1.substring(1), s2))
            :
        !s1.isEmpty() && s2.charAt(0) == s1.charAt(0) ?
            samePattern(s1.substring(1), s2.substring(1))
            :
        false;
}
public static void main(String[] args) {
    String[] patterns = {
        "The*xamIs*y",    // true
        "Th*mIsEasy*",    // true
        "*",              // true
        "TheExamIsEasy",  // true
        "The*IsHard",     // false
    };
    for (String pattern : patterns) {
        System.out.println(samePattern("TheExamIsEasy", pattern));
    }
}

The algorithm

Essentially here's the recursive definition:

  • If s2 is empty, then it's samePattern if s1 is also empty
  • Otherwise s2 is not empty
    • If it starts with *, then it's samePattern if
      • It's samePattern with the * removed
      • Or it's samePattern with a character removed from s1 (if there's one)
    • Otherwise it starts with a regular character
      • If it matches the first character s1, then check if it's samePattern for the rest of s1, s2
      • Otherwise it's not samePattern, so it's false

Simplified version

Here's the simplified version of the above algorithm:

static boolean samePatternSimplified(String s1, String s2) {
    if (s2.length() == 0) {
        return s1.length() == 0;
    } else if (s2.charAt(0) == '*') {
        return samePatternSimplified(s1, s2.substring(1))
           || (s1.length() != 0 && samePatternSimplified(s1.substring(1), s2));
    } else if (s1.length() != 0 && s2.charAt(0) == s1.charAt(0)) {
        return samePatternSimplified(s1.substring(1), s2.substring(1));
    } else {
        return false;
    }
}

API links

polygenelubricants
Thank you very much for assisting me. However, you used for loop and it's not allowed to use loops in this exercise. Also what's pattern : patterns? I didn't learn about it, so it's now allowed to use it either. Also I don't see the code of the method isEmpty? It's not allowed to use an existing method it's obligatory to write it.
Oh, you wrote for in the main method. Ok. But what about the IsEmpty method? Can you please write it?
My teacher said there are many "stop" conditions. Are you sure you checked all the possibilites?
you can replace `s1.isEmpty()` with `s1.length() == 0`
Andrei Fierbinteanu
@annayena: now no loop.
polygenelubricants
Ok, what about the "stop" conditions? There is no more conditions to check except what you wrote?
Oh, I haven't even noticed you wrote the while loop in the method before. Thanks for removing it so fast.
What now? where are the ifs? I didn't learn about the ? and : things. Please return it to what it was before without the while.
@annayena: I'm not here to do your coursework. I'm here to show you how to learn. Now, learn.
polygenelubricants