796. 旋转字符串『简单』

题目来源于力扣(LeetCode

一、题目

796. 旋转字符串

说明:

  • AB 长度不超过 100

二、解题思路

  1. 字符串 A 拼接两次后形成的字符串中一定包含了旋转后的字符串

  2. 两个字符串长度一致字符串 B 存在于字符串 A 拼接两次后形成的字符串中时,返回 false

三、代码实现

public static boolean rotateString(String A, String B) {
    return A.length() == B.length() && (A + A).contains(B);
}

四、执行用时

五、部分测试用例

public static void main(String[] args) {
    String A = "abcde", B = "cdeab";  // output: true
//    String A = "abcde", B = "abced";  // output: false

    boolean result = rotateString(A, B);
    System.out.println(result);
}
原文地址:https://www.cnblogs.com/zhiyin1209/p/13227536.html