Friday, August 9, 2013

Coding Exercises Day 7 (Problem #8)

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 if s2 is a rotation of s1 using only one call to isSubstring.  E.g., "waterbottle" is a rotation of "erbottlewat".
boolean isRotation(String s1, String s2) {
  try {
    if (s1.length() != s2.length()) {
      return false;
    }
    String concatString = s2 + s2;

    return s1.isSubstring(s2);
  }
  catch (NullPointerException npe) {
    npe.printStackTrace();
    return false;
  }
}

No comments:

Post a Comment