首页 > 试题广场 >

旋转字符串

[编程题]旋转字符串
  • 热度指数:12974 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
字符串旋转:
给定两字符串A和B,如果能将A从中间某个位置分割为左右两部分字符串(可以为空串),并将左边的字符串移动到右边字符串后面组成新的字符串可以变为字符串B时返回true。

例如:如果A=‘youzan’,B=‘zanyou’,A按‘you’‘zan’切割换位后得到‘zanyou’和B相同,返回true。
再如:如果A=‘abcd’,B=‘abcd’,A切成‘abcd’和''(空串),换位后可以得到B,返回true。

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

输入

"youzan","zanyou"

输出

true
示例2

输入

"youzan","zyouan"

输出

false
示例3

输入

"nowcoder","nowcoder"

输出

true
/**
 * 旋转字符串
 * @param A string字符串
 * @param B string字符串
 * @return bool布尔型
 */
bool solve(char* A, char* B ) {
    // write code here
    int lenA = strlen(A);
    char *Arry = (char*)malloc(lenA*sizeof(char));
   
    int i = 0,j = 0;
    for(i=0;i<lenA;i++)
    {
        for(j=0;j<=i;j++)
        {
            Arry[lenA-1-i+j] = A[j];
        }
        for(j=i+1;j<lenA;j++)
        {
            Arry[j-i-1] = A[j];
        }
        int cmp = strcmp(Arry, B);
        if(!cmp) return true;
    }
   
    return false;
}
发表于 2021-08-25 10:37:52 回复(0)

问题信息

上传者:牛客332641号
难度:
1条回答 8912浏览

热门推荐

通过挑战的用户

查看代码