Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Determing if one string is a rotation of another

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Thanked 0 Times in 0 Posts

    Default Determing if one string is a rotation of another

    I have an assignment for my computer science 1 class I could really use some help on. Here's exactly what it says:

    Assume you have a method isSubstring which checks if one word is a substring of another, ex: isSubstring("water", "ter") returns true.
    Given two strings s1 and s2, write code to check if s2 is a rotation of s1 using isSubstring, ex: "waterbottle" is a rotation of "erbottlewat"

    I figured a way to put all the characters into an array and then just shuffle the order one letter at a time and compare that to the original. Problem is that the code has to use the isSubstring method mentioned. Any ideas?

  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    My Mood
    Thanked 167 Times in 158 Posts

    Default Re: Determing if one string is a rotation of another

    You need to find where a substring of the first String is a substring of the second String. For example:

    Is waterbottle a substring of erbottlewat? No.
    Is aterbottle a substring of erbottlewat? No.
    Is terbottle a substring of erbottlewat? No.
    Is erbottle a substring of erbottlewat? Yes.

    Now you have a location you can chop the first String in two at that location, concatenate them back together in the opposite order and compare to the second String.
    Improving the world one idiot at a time!

Similar Threads

  1. Replies: 1
    Last Post: April 19th, 2012, 02:46 AM
  2. Left and Right rotation in a balanced binary tree problem
    By szuwi in forum Algorithms & Recursion
    Replies: 2
    Last Post: December 22nd, 2010, 06:20 PM
  3. Basic image rotation method
    By SSully in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 15th, 2010, 01:22 AM
  4. Pixel Alteration in an image/image rotation
    By bguy in forum Java Theory & Questions
    Replies: 3
    Last Post: November 1st, 2009, 09:50 AM
  5. Problem with sprite rotation in graphics
    By Katotetu in forum Algorithms & Recursion
    Replies: 0
    Last Post: May 8th, 2009, 07:27 PM