Trying to implement a recursive algorithm that determines if a string is an ordered shuffle of two other strings

时间:2015-11-18 21:02:10

标签: java algorithm recursion shuffle

Basically i am trying to create a recursive algorithm that can check if a String is an ordered shuffle of two other Strings.

An ordered shuffle "String c" can be formed by iterspersing the characters 
of String a and String b in a way that maintains the left to right order of
the characters from each String. 

I have attempted to create one however get errors where the first characters of Strings a and b are the same.

Any feedback on how to improve my algorithm would be very much appreciated.

public class ShuffleTest {

public static boolean isOrderedShuffle(String a, String b, String c){
    boolean isOrdered = false;
    int n = a.length();
    int m = b.length();
    int len = c.length();

    if (len != (n + m)){
        return isOrdered;
    }

    if (len == 1){
        if (c.equals(a) || c.equals(b)){
            return true;
        }
    }

    if (c.charAt(0) == a.charAt(0)){
        a = a.substring(1, n);
        c = c.substring(1, len);
            return isOrderedShuffle(a, b, c);
    }

    else
        if (c.charAt(0) == b.charAt(0)){
            b = b.substring(1, m);
            c = c.substring(1, len);
                return isOrderedShuffle(a, b, c);
        }

        else 
            return isOrdered;

}

public static void main(String[] args) {

    //Prints false when should be true.
    System.out.println(ShuffleTest.isOrderedShuffle("italian", "university", "italuniversityian"));

}

}

2 个答案:

答案 0 :(得分:2)

我不熟悉Java,但我希望这足够可读:

def ordered_shuffle(a, b, c):
    if c:                          # c is not empty
        if a and b and a[0]==b[0]==c[0]: # if first char of c is the same as both first chars of b and c
            return ordered_shuffle(a[1:], b, c[1:]) or ordered_shuffle(a, b[1:], c[1:])
        elif a and (a[0] == c[0]):   # if a is not empty and first char of a == first char of c
            return ordered_shuffle(a[1:], b, c[1:]) # skip 1 char of a and c
        elif b and (b[0] == c[0]): # if b is not empty and first char of b == first char of c
            return ordered_shuffle(a, b[1:], c[1:]) # skip 1 char of b and c
        else:
            return False
    else:
        return not a and not b     # if c is empty, return True if a and b are also empty

示例:

print(ordered_shuffle("abc","def","adbcef"))
a=abc b=def c=adbcef
a=bc b=def c=dbcef
a=bc b=ef c=bcef
a=c b=ef c=cef
a= b=ef c=ef
a= b=f c=f
a= b= c=
True

print(ordered_shuffle("abc","def","acdebf"))
a=abc b=def c=acdebf
a=bc b=def c=cdebf
False

print(ordered_shuffle("cat","castle","castlecat"))
a=cat b=castle c=castlecat
a=at b=castle c=astlecat
a=t b=castle c=stlecat
a=cat b=astle c=astlecat
a=cat b=stle c=stlecat
a=cat b=tle c=tlecat
a=cat b=le c=lecat
a=cat b=e c=ecat
a=cat b= c=cat
a=at b= c=at
a=t b= c=t
a= b= c=
True

答案 1 :(得分:1)

在检查前尝试这样的事情:

 if (c.charAt(0) == a.charAt(0) && c.charAt(0) == b.charAt(0)){
    String a2 = a.substring(1, n);
    String b2 = b.substring(1,n);
    c = c.substring(1, len);
        return isOrderedShuffle(a2, b, c) || isOrderedShuffle(a, b2, c);
}