|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
【问题描述】
字符串的旋转是指:把字符串前面的若干字符移动到字符串的尾部,比如:把字符串"abcdef"前2位字符移动到后面得到字符串为“cdefab“,可以把字符串看成两段,分别即为XY,左旋转相当于把字符串XY变成YX。编写一个函数,判断字符串s1是否由字符串s2旋转而来,函数头:is_rotation(s1,s2)
【输入形式】
分行输入两个字符串
【输出形式】
成立输出True,不成立输出False
【样例输入】
stringbook
bookstring
【样例输出】
True
本帖最后由 连帅帅 于 2021-6-30 17:46 编辑
java版本的:
- package com.lian.controller;
- import java.util.Scanner;
- /**
- * @author :LSS
- * @description: String rotation
- * @date :2021/6/30 17:28
- */
- public class Test {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String next = scanner.next();
- String next1 = scanner.next();
- int length = next.length();
- int i1 = length / 2;
- if (length % 2 == 0) {
- next = next.substring(i1) + next.substring(0, i1);
- } else {
- next = next.substring(i1 + 1) + next.substring(i1, i1 + 1) + next.substring(0, i1);
- }
- if (next.equals(next1)){
- System.out.println("True");
- }else{
- System.out.println("False");
- }
- }
- }
复制代码
|
|