首页 > 试题广场 >

旋转字符串

[编程题]旋转字符串
  • 热度指数:13072 时间限制: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
    public boolean solve (String A, String B) {
        // write code here
        int len=A.length();
        for(int i=0;i<len;i++){
            if(A.charAt(i)==B.charAt(0)){
                if((A.substring(i,len)+A.substring(0,i)).equals(B)){
                    return true;
                }
            }
        }
        return false;
    }

编辑于 2024-03-24 14:49:51 回复(0)
简单粗暴(从A的开头取1,2,3.. 加到末尾判断是否和B相等)
import java.util.*;


public class Solution {
    /**
     * 旋转字符串
     * @param A string字符串
     * @param B string字符串
     * @return bool布尔型
     */
    public boolean solve (String A, String B) {
        // write code here
        if (A.equals(B)) {
            return true;
        }
        // boolean flag =false;
        StringBuilder sb = new StringBuilder();

        for (int i = 1; i < A.length(); i++) {

            String temp1 = A.substring(0, i);
            String temp2 = A.substring(i, A.length());

            sb.append(temp2).append(temp1);
            
            if (sb.toString().equals(B)) {
                return true;
            } else {
                sb = new StringBuilder();
            }
        }
        return false;

    }
}

发表于 2022-10-05 19:45:33 回复(0)
遍历A中的起始位置,查看与B起始位置相同的位置,若存在,则继续匹配,若完全匹配则,返回true,否则继续在A中寻在下一个匹配位置。
发表于 2022-07-09 21:08:17 回复(0)
import java.util.*;


public class Solution {
    /**
     * 旋转字符串
     * @param A string字符串 
     * @param B string字符串 
     * @return bool布尔型
     */
    public boolean solve (String A, String B) {
      if(A==null||B==null){
        return false;
    }
    if((A.length()&1)==1){
        return false;
    }
    if(A.length()-B.length()!=0){
        return false;
    }
    int halflength=A.length()/2;
    char[] achars=A.toCharArray();
    char[] bchars=B.toCharArray();
    if(achars[0]!=bchars[halflength]){
      return false;
    }
    if(achars[halflength]!=bchars[0]){
      return false;
    }
    if(!B.contains(A.substring(0,halflength-1))){
        return false;
    }
    if(!B.contains(A.substring(halflength,A.length()-1))){
        return false;
    }
    return true;
    }
}
发表于 2022-07-03 10:57:47 回复(0)
public boolean solve (String A, String B) {
        // write code here
        if(A.equals(B)){
            return true;
        }
        if(A.length() != B.length()){
            return false;
        }
        int i;
        int m = 0;
        char first = B.charAt(0);
        String lefthalf = "";
        String righthalf = "";
        //这里已经排除第一个字符相等的情况,所以这种情况的话
        //虽然第一个字符相等,但是此时还需要一直往下面遍历
        //直到遍历到第二次相同时
        if(A.charAt(0) == B.charAt(0)){
                m = 1;
         }
        //从A字符串中找到对应的字符的位置
        for(i = m ;i < A.length(); i++){
            if(A.charAt(i) == first){
                break;
            }
            lefthalf += A.charAt(i);
        }
        //必须把落下的补回
        if(m == 1){
            lefthalf = String.valueOf(A.charAt(0)) + lefthalf ;
        }
        //遍历后半部分
        for(int j = i ; j < A.length() ; j ++){
            righthalf += A.charAt(j);
        }
        String res = righthalf + lefthalf ;
        if(res.equals(B)){
            return true;
        }
        return false;
    }

发表于 2022-01-08 20:50:29 回复(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).contains(B);
    }
}

发表于 2021-08-19 15:36:31 回复(3)
       public boolean solve (String A, String B) {
            // write code here
           if (A.length()!=B.length())return false;
            char[] c1=A.toCharArray();
            char[] c2=B.toCharArray();
            //找到B中A的首字符
            int pos=0;
            for (int i=0;i<c2.length;i++){
                if (c2[i]==c1[0]){
                    pos=i;
                    break;
                }
            }
            ArrayList<Character> cha=new ArrayList<>();
            for (int i=pos;i<c2.length;i++){
                cha.add(c2[i]);
            }
            for (int i=0;i<pos;i++){
                cha.add(c2[i]);
            }
            for (int i=0;i<c1.length;i++){
                if(c1[i]!=cha.get(i))return false;
            }
            return true;
        }

发表于 2021-07-25 22:43:58 回复(0)
public boolean solve (String A, String B) {
    // write code here

    String temp1;
    String temp2;
    int length = A.length();
    for (int i=0; i<A.length(); i++) {
        temp1 = A.substring(0, i);
        temp2 = A.substring(i);
        if ((temp2 + temp1).equals(B)) {
            return true;
        }
    }

    return false;
}

编辑于 2021-07-24 17:19:47 回复(0)
    public boolean solve (String A, String B) {
        if(A.length()!=B.length()) return false;
        String first=""+B.charAt(0);
        if(!A.contains(first)) return false;
        int pos=A.indexOf(first);
        int i=pos,j=0,n=A.length();
        while(j<n&&A.charAt(i)==B.charAt(j)){
            i++;j++;
            if(i==n) i=0;                                   
         }
        if(j<n) return false;
        return true;
    }

发表于 2021-03-30 16:07:31 回复(0)