Wednesday, May 25, 2016

Amazon Interview Question: Assume you have a method isSubstring which checks if one word is a substring of another.

Let say you have two string s1 and s2 ,write code to check if s2 is a rotation of s1 using only one call to isSubstring(eg. "WaterBottle" is a rotation of "erbottlewat").

Approach First:

lets take s1 into two parts x and y in rotation.

s1= xy = waterbottle
x= wat
y=erbottle
s2=yx=erbottlewat

Solution:

public boolean isRotation(String s1, String s2)
{
  int len =s1.length();
  if(len == s2.length() && len > 0)
  {
    String s1s1=s1+s1;
    return isSubstring(s1s1,s2);
  }
  return false;
}


Amazon Interview Question............................................... :)

No comments: