Mickd94
Mickd94

Reputation: 63

Need advice solving recursive algorithm for determining if a string is a shuffle of two other strings

I am trying to create a recursive algorithm that determines if a String c is an ordered shuffle of Strings a and b. An ordered shuffle means that String c is made up by interspersing the characters of String a and b in a way that still maintains the left to right order of the two strings.

I have attempted the code for this algorithm and it works to some extent, however i have come across a problem when trying to test for a certain shuffled word as can be seen in my code below. I believe this is something to do with index errors in my if statements, however cannot figure out a way to fix this statement or go around it, any feedback or guidance would be very much appreciated.

public class StringShuffleTest {

    public static boolean isOrderedShuffle(String a, String b, String c){
        //boolean to determine if String c is an ordered shuffle.
        boolean isShuffled = false;
        //variables for the size of Strings a, b and c.
        int n = a.length();
        int m = b.length();
        int len = c.length();

        //if the length of c is not the length of a + b return false.
        if (len != (n + m)){
            return isShuffled;
        }

        //if the length of a or b is 0, and c equals a or b, return true, otherwise,
        //return false.
        if (n == 0 || m == 0){
            if (c.equals(a) || c.equals(b)){
                return true;
            }
            else
                return isShuffled;
        }

        //if String a has length 1, remove String a from String c and make String a empty.
        if (n == 1){
                c = c.substring(0, c.indexOf(a.charAt(0))) + c.substring(c.indexOf(a.charAt(0)) +1);
                a = "";
                return isOrderedShuffle(a, b, c);

            }

        else
        //Recursive algorithm to determine if String c is an ordered shuffle of a and b.
        if (c.indexOf(a.charAt(0)) >= 0){

            int indexOfFirst = c.indexOf(a.charAt(0));
            int indexOfSecond = c.indexOf(a.charAt(1));

            if (indexOfFirst <= indexOfSecond){
            c = c.substring(0, indexOfFirst) + c.substring(indexOfFirst +1);
            a = a.substring(1, n);
                System.out.println(a);
                System.out.println(c);                   
            return isOrderedShuffle(a, b, c);
            }
            else
                return isShuffled;
        }

    return isShuffled;         
    }       

public static void main(String[] args) {

    System.out.println(StringShuffleTest.isOrderedShuffle("castle", "cat", "catcastle")); 

}

}

Upvotes: 0

Views: 236

Answers (1)

maraca
maraca

Reputation: 8753

You could simplify this by creating a method which calls the recursive method, e.g.:

private static String a, b, c;

public static boolean isOrderedShuffle(String a, String b, String c) {
    if (a.length() + b.length() != c.length())
        return false;
    StringShuffleTest.a = a; StringShuffleTest.b = b; StringShuffleTest.c = c;
    return isOrderedShuffle(0, 0);
}

private static boolean isOrderedShuffle(int n, int m) {
    if (n + m == c.length())
        return true;
    if (n < a.length() && a.charAt(n) == c.charAt(n + m) && isOrderedShuffle(n + 1, m))
        return true;
    if (m < b.length() && b.charAt(m) == c.charAt(n + m) && isOrderedShuffle(n, m + 1))
        return true;
    return false;          
}

Upvotes: 2

Related Questions