Thursday, August 20, 2015

Rotated Strings

Problem

Assume you have a method isSubstring which checks if one word is a substring
of another. Given two strings, s1 and s2, write code to check Ifs2 is a rotation of s1
using only onecalltoisSubstring (e.g., "waterbottLe" is a rotation of "erbottLewat").

Note: this is from Cracking the Coding Interview

Solution

 public boolean isRotation(String s1, String s2){
  if(s1.length() != s2.length() || s2.length() == 0) return false;
  return isSubString(s1 + s1, s2);
 }

No comments:

Post a Comment