0

Given three strings a, b and c. Trying to check whether c is an interspersed version of a and b, recursively.

So an example would be.

a = dolphin
b = whale
c = dolwhphialne

You can see that the chars in the c string are in the same order as they would be in their original strings, just shuffled up a little bit.

The algorithm I'm working on is.

Algorithm: stringOrderedCheck(a,b,c)
    n:= length of a
    m:= length of b
    nm:= length of a and b
    if (nm > 0) then
        if(first letter of a equals first letter of c) then
            return stringOrderedCheck(a without first letter,b, c without first letter)
        else if(first letter of b equals first letter of c) then
            return stringOrderedCheck(a,b without first letter, c without first letter)
    else 
        return false;

But the problem that I am getting is this for example:

a = yyyyb
b = yyyx
c = yyyyxyyyb

How do I prepare the function to realise that it needs to take the first letter from a which is y then take the next 4 from b which is yyyx.

Duplicate values are causing me a major issue.

I need to do something when both chars in a and b equal the char in c

2
  • length of a and b What are you calculating here? The sum? The minimum? Commented Nov 10, 2015 at 22:37
  • the length of a and b's strings combined @Rhymoid Commented Nov 10, 2015 at 22:39

1 Answer 1

1

I believe that the main problem here is that, at this point, your path can split into two separate recursive calls, ie the tree divides rather than just going deeper. the problem is that because of your else if, the second path is only ever even considered if the first path is not, ie, it never considers both. you can then prepare for this:

#your original code
if(first letter of a equals first letter of c) then
        return stringOrderedCheck(a without first letter,b, c without first letter)
else if(first letter of b equals first letter of c) then
        return stringOrderedCheck(a,b without first letter, c without first letter)

you can change this into:

if(first letter of a equals first letter of c) then
    if(first letter of b equals first letter of c) then
        return stringOrderedCheck(a without first letter,b, c without first letter) OR stringOrderedCheck(a,b without first letter, c without first letter)
    else
        return stringOrderedCheck(a without first letter,b, c without first letter)
else if(first letter of b equals first letter of c) then
        return stringOrderedCheck(a,b without first letter, c without first letter)

so that it checks for both regardless of the condition of the first one.

you can also change this by saving your bool as a variable before the if checks then asserting a general or:

valid:= false
if(first letter of a equals first letter of c) then
        valid = valid or stringOrderedCheck(a without first letter,b, c without first letter)
if(first letter of b equals first letter of c) then #take out the else so it will always check
        valid = valid or stringOrderedCheck(a,b without first letter, c without first letter)
return valid

this last solution shows a pretty important note: make sure you have an else after your if/else if statement that returns false (if you dont use the last solution) to account for if the first letter of c is straight up just not valid (ie not the first letter of a or b)

Sign up to request clarification or add additional context in comments.

9 Comments

Minor detail to note: this algorithm works best when the or-operator is short-circuiting. That is, if the expression on the left hand side evaluates to true, the expression on the right hand side is never evaluated. This may save quite a few unnecessary searches.
Thank you for the answer, I will attempt to implement it.
@Rhymoid good point, because im used to python, I had assumed that this was the case but I forgot that this was just a general pseudocode :P
for some reason I'm getting an error when one of the strings is empty @Rhymoid
its kind of hard to assess how to fix that with pseudo code but assuming you are doing something like a[0] to grab the first letter, if a is an empty string, that would cause an out of index error. i suggest doing an initial check if a/b are empty and working with that accordingly
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.