首页 > 试题广场 >

旋转字符串(二)

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

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

输入

"abcd","bcda"

输出

true
示例2

输入

"abcd","abcd"

输出

false

方法1:暴力

import java.util.*;
public class Solution {
    public boolean solve (String A, String B) {
        if(A.length() != B.length()) return false;
        for(int i = 1; i < A.length(); ++i) {
            if(B.endsWith(A.substring(0, i)) && B.startsWith(A.substring(i))) return true;
         }
        return false;
    }
}

方法2:

import java.util.*;
public class Solution {
    public boolean solve (String A, String B) {
        if(A.length() != B.length()) return false;
        return (A+A).substring(1, 2*A.length() - 1).contains(B);
    }
}
发表于 2023-06-07 15:45:17 回复(0)
class Solution {
public:
    /**
     */
    bool solve(string A, string B) {
        A += A;
        if (A.find(B) == string::npos || A.find(B, 1) == A.length() / 2) {
            return false;
        } else {
            return true;
        }
    }
};

发表于 2023-05-24 17:41:01 回复(0)
package main
import _"fmt"

/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 旋转字符串
 * @param A string字符串 
 * @param B string字符串 
 * @return bool布尔型
*/
func solve( A string ,  B string ) bool {
    cnt:=0
    for {
        A=A[1:]+A[:1]
        cnt++
        if A==B{
            return true
        }
        if cnt+1>=len(A){
            return false
        }
    }
    return false
}

发表于 2023-03-11 00:34:53 回复(0)
class Solution:
    def solve(self , A , B ):
        # write code here
        if len(A) != len(B):
            return False
        for i in range(1,len(A)):
            l, r = A[:i], A[i:]
            if r+l == B:
                return True
        return False

发表于 2022-04-27 08:59:35 回复(0)
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 旋转字符串
# @param A string字符串 
# @param B string字符串 
# @return bool布尔型
#
class Solution:
    def solve(self , A , B ):
        # write code here
        if len(A) != len(B):
            return False
        for i in range(1,len(A),1):
            if A[0:i] == B[len(B)-i:] and A[i:] == B[0:len(B)-i]:
                return True
        return False

发表于 2022-03-20 11:30:19 回复(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)
class Solution:
    def solve(self , A , B ):
        # write code here
        for i in range(1, len(A)):
            str1 = A[i:] + A[:i]
            if str1 == B:
                return True
                break
        return False
发表于 2021-08-15 02:09:15 回复(0)
class Solution:
    def solve(self , A , B ):
        # write code here
        a_list=list(A)
        n=int(len(A)/2)
        new_A=A[n:]+A[:n]
        if new_A==B:
            return True
        else:
            return False
发表于 2021-08-11 21:42:25 回复(0)