0

Given 2 strings, s1 and s2. Len(s2) < len(s1).

Both strings have only “a” and “b” characters. Given 2 operations:

  • s2 = s2 + “a”
  • s2 = rev(s2) + “b”

Find if by doing the above 2 operations, s2 will be equal to s1 ever.

I can only think of the naive solution where we can recursively perform the two mentioned operations on s2 and whenever length of the two strings become equal we can check if s2 = s1.

But the time complexity of the naive solution is exponential : (2^len_diff), where len_diff is the difference of the lengths of the two strings.

Is there a better solution for this?

2
  • Is this from a coding contest? Commented Nov 9, 2020 at 17:34
  • @abhay no this is not from any contest. I was asked this question in an interview. Commented Nov 10, 2020 at 1:38

2 Answers 2

2

Work backwards. Instead of building s1 from s2, strip characters from the end of s1. This way you know for sure which operation to undo. If you eventually end up with s2, you are done.

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

1 Comment

This seems to be the simplest answer, and there also does not seems to be anything wrong with it. Can someone confirm if this is the correct answer?
0

So assuming the answer exists, s1 will either be of form:

(s2a*), (a*bs2a*ba*), (a*ba*bs2a*ba*ba*),...

or of form

(a*s'2ba*), (a*ba*s'2ba*ba*),...

where s'2 is the reverse of s2


So your question reduces to finding whether the regular expression

s1 = [(a*b)n s2 (a*b)n a*] V [a* (ba* )n s'2 (ba*)n+1]

holds true. You could use a regex library to parse and check it


If have to write the parsing algorithm ground up, you wouldn't even have to check for a*. You could get away with merely counting the number of b's to either side of s2

A pseudocode would be something like

B1 = number of b's in s2 //O(n)
B2 = number of b's in s1 //O(n)

//Locate s2 in s1 and check b_left = b_right and character before s2 is b

b_to_left = 0

for i from 1 to (s1's length - s2's length): //O(n iteration)
    if s2.length characters of s1 starting at index i DO NOT match s2: //At worse O(n to check)
        if(s1[i] is b) b_to_left++
        continue to next i

    b_to_right = B1 - B2 - b_to_left
    if(b_to_left == b_to_right) :
        if(b_to_left == 0)  RETURN TRUE
        else if(s[i-1] is b) RETURN TRUE
    
    if(s1[i] is b) b_to_left++


//Similarly run another round and parse for the second type involving reverse of s2
//I'm leaving that out as an exercise for you since you mentioned it's an interview problem 




RETURN FALSE


Time: O(n2)

Space: O(1)

1 Comment

Yeah it seems to be the correct approach, just wondering if the answer by user58697 is correct, in that case that would be the simplest answer with O(n) time complexity. Also the interview was over before I posted the question. I could not answer in the interview.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.