0

I'm new to writing algorithms, i am making a program that checks if a string is an ordered shuffle of 2 other strings. (ordered from left to right) for example

string 1 = dog
string 2 = cat
string shuffle = dcoagt

here is my algorithm

**Algorithm: orderedSort(a,b,c)**
given strings a,b,c determine whether c is an ordered shuffle of a and b

l := length of (a + b)
if (l = 0) then
    return true
if (l not = length of c)
    return false
else
    d := substring of a position 1 to 1
    e := substring of b position 1 to 1
    f := substring of c position 1 to 1
if (d = f) then 
    return orderedSort(a-d, b, c-f) 
if (e = f) then
    return orderedSort(a, b-e, c-f)
if (d and e = f) then
    return orderedSort(a-d, b, c-f) or orderedSort(a, b-e, c-f)

My question is, is this correct (if not, suggestions on how to write it) as i was not sure how i would write pseudo code for the last if statement, as the recursion can go either way.

Or do i not need to write the last if statement and the programmer would include it when making the program so it doesn't fail in the case of

string 1 = aa
string 2 = aaf
string combination = aafaa

my Java implementation of this is

public class OrderedSort {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        String a = "xxyxxy";
        String b = "xxzxxz";
        String c = "xxzxxyxxyxxz";
        System.out.println(c + " is an ordered sort of " + a + " and " +  b + " ... " +  ordered(a,b,c));      
    }

    public static boolean ordered(String a, String b, String c){
        if(!((a.length()+b.length())==c.length()))
            return false;
        if(c.length()== 0)
            return true;
        if(a.length()>0 && b.length()>0 && a.substring(0,1).equals(b.substring(0,1)))
            return ordered(a.substring(1),b,c.substring(1)) || ordered(a,b.substring(1),c.substring(1));
        if(a.length()>0 && a.substring(0,1).equals(c.substring(0,1)))    
            return ordered(a.substring(1),b,c.substring(1));
        if(b.length()>0 &&b.substring(0,1).equals(c.substring(0,1)))
            return ordered(a,b.substring(1),c.substring(1));
        return false;
    }

}
  • What language is this in? – Jonathan Lam Nov 15 '15 at 15:19
  • This is a pseudo-code algorithm, but i have made the program in java based on the algorithm if you want to see it? @Jonathan –  Nov 15 '15 at 15:20
  • @simarstar That's not Java ... is that Pascal? And I edited your question to show that – Jonathan Lam Nov 15 '15 at 15:23
  • i wasn't writing it for a specific language, i was writing it so it could hopefully be implemented into any language. If you look at my edit it has the java in, would the algorithm be a accurate representation of it? @Jonathan –  Nov 15 '15 at 16:00

4 Answers4

0

What you wrote it's also correct, however if you write

return max( orderedSort(a-d, b, c-f), orderedSort(a, b-e, c-f))

That could be more clear.

Máté Juhász
  • 2,197
  • 1
  • 19
  • 40
0

The logic behind your idea is correct but there is no need to pass substrings to the recursive functions. By keeping a pointer for every string we can complete the task. This is my C++ implementation:

#include<iostream>
using namespace std;

string a = "aa";
string b = "aaf";
string c = "aafaa";

bool check( int p1, int p2, int p3 ){
    bool c1 = p1 < a.size();
    bool c2 = p2 < b.size();
    bool c3 = p3 < c.size();
    if ( !c1 && !c2 && !c3 ){
        return true;
    }
    if ( c1 && c2 && c3 && a[p1]==b[p2] && b[p2]==c[p3] ){
        bool b1 = check(p1+1,p2,p3+1);
        bool b2 = check(p1,p2+1,p3+1);
        return (b1 || b2);
    } else if ( c3 && c1 && a[p1] == c[p3] ) {
        return check(p1+1,p2,p3+1 );
    } else if ( c2 && c3 && b[p2] == c[p3] ) {
        return check(p1,p2+1,p3+1 );
    } else {
        return false;
    }
}


int main()
{
    cout << check(0,0,0) << endl;
}
Saeid
  • 4,147
  • 7
  • 27
  • 43
0

Some Ruby code here (is imho pseudocode on it self).

# with two strings
s1, s2 = 'abc', 'defg'
s1.split('').shuffle.join + s2.split('').shuffle.join
# "cbagefd"

# with variable number of strings
%w[abc defg hijkl].map{|string| string.split('').shuffle.join}.join
# "bacefdghlijk"

No recursion necessary, you shuffle every string on its own and concatenate (join) the together.

Some explanation of the code:

.split(''): splits the string in an array of characters
.shuffle and .join speak for them self
%w[...] spilts the contained string in array of strings separated by a space
.map makes an array of the contained block of code
peter
  • 41,770
  • 5
  • 64
  • 108
  • i know it doesn't need to be written recursively, but that is what i am practicing. –  Nov 15 '15 at 16:02
0

Easier approach : count all the occuriencies of characters in the strings you start with and put them in an array indexed by character ascii number. Now, when you get a new string, check if you could make the total sum of the differences between each character = 0.

Christo S. Christov
  • 2,268
  • 3
  • 32
  • 57