首页 > 试题广场 >

旋转字符串(二)

[编程题]旋转字符串(二)
  • 热度指数:2042 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
字符串旋转:
给定两个长度为n字符串A和B,如果能将A从中间某个位置分割为左右两部分字符串(不能为空串),并将左边的字符串移动到右边字符串后面组成新的字符串可以变为字符串B时返回true。
例如:如果A=‘abcd’,B=‘bcda’,A按‘a’‘bcd’切割换位后得到‘bcda’和B相同,返回true。
再如:如果A=‘abcd’,B=‘abcd’,无法切成两个非空串,使得A换位后可以得到B,返回false。

数据范围:A,B字符串长度满足2 \leq n \leq 1000,保证字符串中仅包含小写英文字母和阿拉伯数字
示例1

输入

"abcd","bcda"

输出

true
示例2

输入

"abcd","abcd"

输出

false
import java.util.*;

public class Solution {
    
    public boolean solve (String A, String B) {
        // write code here
        int index = (A + A).indexOf(B, 1);
        return index > 0 && index < A.length();
    }
}

发表于 2025-04-05 08:33:34 回复(0)

一句话的事

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 旋转字符串
     * @param A string字符串 
     * @param B string字符串 
     * @return bool布尔型
     */
    public boolean solve (String A, String B) {
        // write code here
        return A.length()==B.length()&&(A+A).substring(1,2*A.length()-1).contains(B);
    }
}


发表于 2021-08-24 15:16:48 回复(1)

问题信息

难度:
2条回答 5192浏览

热门推荐

通过挑战的用户

查看代码
旋转字符串(二)