Topic
For a string str, the string that is moved from the front to the back is called the spin word of Str. For example str= "12345", its rotational words have "23451", "34512", "45123", "51234". Given two strings A and B, determine whether A and B are mutually rotating words.
Realize
This is a very flattering implementation, adding the original string one at a time, and using a string-contains method to determine whether to include the matched string as judged
import org.junit.Assert;import org.junit.Test;/** * @author lorem */public class IsRotationTest { boolean isRotation(String str1, String str2) { if (str1 == null || str2 == null || str1.length() != str2.length()) { return false; } String str = str1 + str1; if (str.contains(str2)) { return true; } return false; } @Test public void test() { String str1 = "abcd"; String str2 = "dcad"; Assert.assertEquals(true,isRotation(str1, str2)); }}
Determines whether the two strings are mutually rotating words