Google Interview Question

1882
Nov 04, 2024
Nov 04, 2024

Recently I came across a google interview question. The question is as follows:

You are given two strings, P and Q consisting of characters 'a' and 'b'.

At any given point you can perform two kind of operations on the string P:

  1. You either append character a to P.
  2. You append character b to P and then you reverse P.

You can perform any of these operations any number of times.

You have to tell me whether it is possible for P to be equal to Q, after performing such operations?

An example:

  1. P = a, Q = baa. this will return true,
  2. P = b, Q = ab, this will return false

Can someone tell me, how I can approach this question or such questions? Because I am having difficulty in grasping a way on how to proceed?

Comments (18)