判断两个字符串是否互为旋转词

题目

对于一个字符串str,把前面任意部分挪到后面形成的字符串叫作str的旋转词。比如str=”12345”,其旋转词有”23451”、”34512”、”45123”、”51234”。给定两个字符串a和b,判断a和b是否互为旋转词。

实现

这是一种非常讨巧的实现方式,将原始字符串累加一次,并以用String自带的contains方法判断是否包含所判断的是否匹配的字符串

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));
    }
}
原文地址:https://www.cnblogs.com/hoochanlon/p/9736166.html