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

把一个字符串str 前面任意的部分挪到后面形成的字符串叫做str的旋转词

   举例, a="cdab" b="abcd" 返回true

             a="1ab2" b="ab12" 返回false

解法很简单,

  首先长度要一样

  然后 生成一个大字符串b2 为两个字符串b拼在一起的结果

  最后看看 b2中是否包含字符串a

public class Test3 {

    public static boolean isReverse(String str1, String str2) {

        if (str1.length() != str2.length()) {
            return false;
        }

        String string = str1 + str1;
        if (-1 == string.indexOf(str2)) {
            return false;
        }
        return true;
    }

    public static void main(String[] args) {

        String str1 = "abccder";
        String str2 = "cderabc";

        System.out.println(isReverse(str1, str2));

    }

}

如果没有重复的可以这么玩儿:

public class Test3 {

    public static boolean isReverse(String str1, String str2) {
        if (str1.length() != str2.length()) {
            return false;
        }
        int indexOf = str2.indexOf(str1.charAt(0));
        if (indexOf == -1) {
            return false;
        }
        String substring1 = str2.substring(0, indexOf); // cder
        String substring2 = str2.substring(indexOf); // abc
        if ((str1.indexOf(substring1) == indexOf) & (str1.indexOf(substring2) == 0)) {
            return true;
        }
        return false;
    }

    public static void main(String[] args) {

        String str1 = "abcder";
        String str2 = "derabc";

        System.out.println(isReverse(str1, str2));

    }

}
原文地址:https://www.cnblogs.com/toov5/p/7376224.html