We are given two strings, A and B.

A shift on A consists of taking string A and moving the leftmost character to the rightmost position. For example, if A = 'abcde', then it will be 'bcdea' after one shift on A. Return True if and only if A can become B after some number of shifts on A.

Example 1:
Input: A = 'abcde', B = 'cdeab'
Output: true Example 2:
Input: A = 'abcde', B = 'abced'
Output: false
Note: A and B will have length at most 100.
class Solution {
public boolean rotateString(String A, String B) {
if(A == null || B == null || A.length() != B.length()){
return false;
}
if(A.length() == 0 && B.length() == 0){
return true;
} int n = A.length();
     //轮数
for(int i = 0; i< n; i++){
boolean find = true;
       //每一个元素
for(int j = 0; j < n; j++){
int a = A.charAt(j);
int newIndex = (n - i + j) % n;
int b = B.charAt(newIndex);
if(a != b){
find = false;
break;
}
}
if(find){
return true;
}
}
return false;
}
}

LeetCode - Rotate String的更多相关文章

  1. [LeetCode] Rotate String 旋转字符串

    We are given two strings, A and B. A shift on A consists of taking string A and moving the leftmost ...

  2. LeetCode算法题-Rotate String(Java实现)

    这是悦乐书的第317次更新,第338篇原创 在开始今天的算法题前,说几句,今天是世界读书日,推荐两本书给大家,<终身成长>和<禅与摩托车维修艺术>,值得好好阅读和反复阅读. 0 ...

  3. leetcode 344. Reverse String 、541. Reverse String II 、796. Rotate String

    344. Reverse String 最基础的旋转字符串 class Solution { public: void reverseString(vector<char>& s) ...

  4. 796. Rotate String - LeetCode

    Question 796. Rotate String Solution 题目大意:两个字符串匹配 思路:Brute Force Java实现: public boolean rotateString ...

  5. C++ STL@ list 应用 (leetcode: Rotate Array)

    STL中的list就是一双向链表,可高效地进行插入删除元素. List 是 C++标准程式库 中的一个 类 ,可以简单视之为双向 连结串行 ,以线性列的方式管理物件集合.list 的特色是在集合的任何 ...

  6. Rotate String

    Given a string and an offset, rotate string by offset. (rotate from left to right) Example Given &qu ...

  7. Lintcode: Rotate String

    Given a string and an offset, rotate string by offset. (rotate from left to right) Example Given &qu ...

  8. 8. Rotate String

    8. Rotate String Description Given a string and an offset, rotate string by offset. (rotate from lef ...

  9. LeetCode——Reverse String

    LeetCode--Reverse String Question Write a function that takes a string as input and returns the stri ...

随机推荐

  1. Uva LV 2995 Image Is Everything 模拟,坐标映射,视图映射 难度: 1

    题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...

  2. [转]ASP.NET MVC学习系列(二)-WebAPI请求 传参

    [转]ASP.NET MVC学习系列(二)-WebAPI请求 传参 本文转自:http://www.cnblogs.com/babycool/p/3922738.html ASP.NET MVC学习系 ...

  3. VS 中常用的一些快捷键

    一.代码自动对齐 CTRL+K+F     二.撤销/反撤销 1.撤销---使用组合键“Ctrl+Z”进行撤销操作 2.反撤销---使用组合键“Ctrl+Y”进行反撤销操作 三.调用智能提示 使用组合 ...

  4. day7-python打开文件方式

    文件操作 对文件操作流程 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 基本操作 import codecs #主要用来解决乱码问题 f = codecs.open('1. ...

  5. OO第一次课程总结分析

    作为一个之前从未使用过java语言,主攻面向过程式编程的“面向对象”小白,于是乎从第一次作业开始时利用时间疯狂学习java语言,经过三次作业的残酷洗礼,自己对面向对象式编程多多少少有了初步的了解(前路 ...

  6. 8.3 C++格式标识和操纵器

    参考:http://www.weixueyuan.net/view/6409.html 总结: 我们需要借助格式标识符来控制cout对象的输出格式. 在ios_base类中,系统已经定义了很多格式标识 ...

  7. unity3d优化总结篇(二)

    1. 尽量避免每帧处理,可以每隔几帧处理一次 比如: [C#] 纯文本查看 复制代码     function Update() { DoSomeThing(); } 可改为每5帧处理一次: [C#] ...

  8. nginx——绑定 Nginx 进程到不同的 CPU 上

    为什么要绑定 Nginx 进程到不同的 CPU 上 :默认情况下,Nginx 的多个进程有可能跑在某一个 CPU 或 CPU 的某一核上,导致 Nginx 进程使用硬件的资源不均,因此绑定 Nginx ...

  9. spark:ML和MLlib的区别

    ML和MLlib的区别如下: ML是升级版的MLlib,最新的Spark版本优先支持ML. ML支持DataFrame数据结构和Pipelines,而MLlib仅支持RDD数据结构. ML明确区分了分 ...

  10. ecmall 的一些方法说明

    ecmall/eccore /ecmall.php 常量: define('START_TIME', ecm_microtime()); define('IS_POST', (strtoupper($ ...