Reputation: 41
here is the problem we are given three strings.
a=partyrock
b=anthem
c=partyrockanthem
task is to check if string a and b combine can make string c. rule is that you cannot change the order if the letters in the string, but you can jump from one string to another.
so lets says c = pantartyrohemck
in this case you move from one string to another string to make final string.
here is my code for recursion the problem is that i cannot find a way to return the values. if you have easier way to do this please let me know thanks in advance.
public class Hw4c {
public static boolean everyDayImShuffling(String a, String b, String c)
{
boolean result = shufflinga(a,b,c,0,0,0);
return result;
}
public static boolean shufflinga (String a, String b, String c, int d, int e, int f)
{
if(a.substring(d,d+1).equals(c.substring(f,f+1)))
{
if(d!=a.length()-1)
{
d=d+1;
f=f+1;
shufflinga(a,b,c,d,e,f);
}
else
{
if(e!=b.length()-1)
shufflingb(a,b,c,d,e,f);
}
}
else
{
shufflingb(a,b,c,d,e,f);
}
return true;
}
public static boolean shufflingb (String a, String b, String c, int d, int e, int f)
{
if(b.substring(e,e+1).equals(c.substring(f,f+1)))
{
if(e!=b.length()-1)
{
e=e+1;
f=f+1;
shufflingb(a,b,c,d,e,f);
}
else
{
if(d!=a.length()-1)
{
shufflinga(a,b,c,d,e,f);
}
}
}
return true;
}
}
Upvotes: 1
Views: 680
Reputation: 8247
Here is a simple, dynamic statement to do it.
if((a + b).equals(c)) System.out.println("Equal");
Here is a method to accomplish the same thing:
public boolean recursiveEqual(String a, String b, String c) {
if((a + b).equals(c)) return true;
else return false;
}
Upvotes: 0
Reputation: 40661
I will try to design a solution in abstract
checkWords(String a, String b, String combined){
int _a = 0, _b = 0;
for (int i = 0; i < combined.length(); i++){
char c = combined.charAt(i);
if (c == a.charAt(_a))
_a++;
else if (c == b.charAt(_b))
_b++;
else return false;
}
return true;
}
Note this does not include checking for null or empty input strings
It neither does the decision whenever current a and b chars are equal
Upvotes: 3
Reputation: 2243
You can use:
if ((a + b).equals(c)) {
// Equals!
}
Upvotes: 1