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.
-
4Please edit your title to more appropriately summarise the body of the question.Andy E– Andy E2010年06月07日 09:50:59 +00:00Commented Jun 7, 2010 at 9:50
-
10Shouting "URGENT" in your subject is not helping your cause, you know.skaffman– skaffman2010年06月07日 09:51:12 +00:00Commented Jun 7, 2010 at 9:51
-
8The tags should be [Homework] and [Java]. Not [Javadoc]. What have you tried? This is not "Do My Homework For Me.com"S.Lott– S.Lott2010年06月07日 09:51:36 +00:00Commented Jun 7, 2010 at 9:51
-
Wow, it's urgent! I'd better answer it quickly!! Have you tried writing it yourself? If so, post some code. People are more likely to answer homework questions if a) your title is more descriptive than, "I need help!" and b) you show some evidence that you've worked at it yourself.Marcelo Cantos– Marcelo Cantos2010年06月07日 09:53:33 +00:00Commented Jun 7, 2010 at 9:53
-
I am sorry. I am new here I just found this website today. I am stuck on this question and don't have any idea how to solve it or write a method that checks the equality of strings using recursion. I am totally lost please help me. :(user360256– user3602562010年06月07日 09:56:12 +00:00Commented Jun 7, 2010 at 9:56
2 Answers 2
Looks like you might want regular expressions.
The .+ regex pattern is equivalent to your *.
But then you'd have two problems.
3 Comments
.+ means "one or more (+) of any character (.)".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
s2is empty, then it'ssamePatternifs1is also empty - Otherwise
s2is not empty- If it starts with
*, then it'ssamePatternif- It's
samePatternwith the*removed - Or it's
samePatternwith a character removed froms1(if there's one)
- It's
- Otherwise it starts with a regular character
- If it matches the first character
s1, then check if it'ssamePatternfor the rest ofs1, s2 - Otherwise it's not
samePattern, so it'sfalse
- If it matches the first character
- If it starts with
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
String.isEmpty()- Returns
trueif, and only if,length()is0.
- Returns
- JLS 15.25 Conditional Operator ?:
- This is the
?:operator used in the original solution
- This is the
- Java Language Guide/The for-each loop
- This is the
for (String pattern : patterns)construct above
- This is the
8 Comments
s1.isEmpty() with s1.length() == 0